synchronized

Synchronized和Lock区别

不打扰是莪最后的温柔 提交于 2019-12-22 18:31:31
1构成 Synchronized是关键字属于JVM层面。 Lock是具体类(java.util.concurrent.locks.Lock)是API层面的锁。 2使用方法 Synchronized不需要用户去手动释放锁,当Synchronized代码执行完后系统会自动让线程释放对锁的占用。ReentrantLock则需要用户去手动释放锁若没有主动释放锁,就有可能导致出现死锁现象。需要lock()和unlock()方法配合try/finally语句来完成。 3等待是否可中断 Synchronized不可中断,除非抛出异常或者正常运行完成。 ReentrantLock可中断,如(1.设置超时方法tryLock(long timeout,TimeUnit unit) 2.lockInterruptibly()放代码块中,调用interrupt()方法可中断) 4加锁是否公平 Synchronized非公平锁 ReentrantLock两者都可以,默认非公平锁,构造方法可以传入boolean值,true为公平锁,false为非公平锁。 5锁绑定多个条件Condition Synchronized没有 ReentrantLock用来实现分组唤醒的线程们,可以精确唤醒,而不像Synchronized要么随机唤醒一个线程,要么唤醒全部线程。 做个小题来理解Condition

Java并发之内存模型

别等时光非礼了梦想. 提交于 2019-12-22 16:57:35
今天跟大家分享下Java并发之内存模型的知识。 前言 Java内存模型知识包括: 1. 并发问题:原子性、可见性、有序性 2. 顺序一致性内存模型 3. 处理器内存模型 4. 变量存储 : 主存和工作内存 5. 原子性方法:Synchronized、lock 6. 有序性方法: Synchronized ( 内存语义和实现 ( 偏向锁、轻量级锁、重量级锁、其他锁优化措施 ) ) 、Volatile(内存语义、实现(内存屏障)) 7. 可见性:方法 ( Synchronized、Volatile、Lock ) 、Happens - before保证(程序次序规则、锁定规则、 volatile 来源: CSDN 作者: Simon.Qi 链接: https://blog.csdn.net/weixin_45794138/article/details/103653412

公平锁/非公平锁/可重入锁/自旋锁

社会主义新天地 提交于 2019-12-22 14:42:21
在JAVA中我们知道有很多加锁的方式,比如常见的 通过synchronized关键字,还有Lock,还有之前说原子CAS操作时有看到过的死循环方式的自旋锁。 借此来说一下锁的分类: 公平锁: 是指多个线程按照申请的顺序来获取锁,每次获取锁时会先查看此锁维护的等待队列。先到先得。 非公平锁: 是指多个线程获取锁的顺序并不是按照申请锁的顺序,每个线程不停的去获取锁,直到成功,有可能后申请的线程比先申请的线程更先获取锁,高并发情况下,有可能造成优先级反转或者饥饿现象。 常见的非公平锁 synchronized/ReentrantLock (默认创建非公平锁new ReentrangLock(false/true)) 不填默认就是 false 也可以 填写 true 使他变为公平锁。 可重入锁(递归锁): synchronized/ReentrantLock 都是可重入锁 线程可以进入任何一个他已经拥有的锁所同步着的代码块。 指的是同一线程外层函数获得锁之后,在进入内层方法会自动获取锁。可避免死锁 public synchronized method1 ( ) { method2 ( ) ; } public synchronized method2 ( ) { } 像这样的代码就表示了一个可重入锁。获取了method1方法的锁之后调用method2会自动获取锁。

Synchronized and the scope of visibility

拜拜、爱过 提交于 2019-12-22 10:37:47
问题 I've been reading up on Java concurrency and had forgot the fact that synchronization blocks in two threads using the same lock also affect the visibility of variables, even though they were not defined as "volatile". If I have code like this Object lock = new Object(); boolean a = false, b = false, c = false; void threadOne() { a = true; synchronized(lock) { b = true; } c = true; } void threadTwo() { while (true) { synchronized(lock) { if (a && b && c) break; } } } ... and threadOne and

Concurrent code analyzer

佐手、 提交于 2019-12-22 08:47:17
问题 I was wondering if there is any framework or application(app)/program out there that can analyze the concurrency of any java code? If the tool knows all the implementations of the jre shipped classes and methods then it comes down to a simple analyzing of synchronized blocks and method and their call hierarchies. From here it can create a petri net and tell you for sure if you could ever experience a deadlock. Am I missing out on something or is this really so easy? Then there must be some

PHP pthreads - shared objects

﹥>﹥吖頭↗ 提交于 2019-12-22 06:56:04
问题 Im searching a safe and fast way to use a shared object. I asked the question already here: https://github.com/krakjoe/pthreads/issues/470 but obviuously this wasnt the right place. Trying to share an object (Threaded) with many other contextes (Thread). All threads are updating this shard object -- they can set own requests and have to respond to requests from others also. Now that krakjoe responded that lock/unlock wont be available in 7 i got a problem. I know about :.synchronized but have

Java - Synchronized methods causes program to slow down massively

吃可爱长大的小学妹 提交于 2019-12-22 05:39:15
问题 I'm trying to learn about threads and synchronization. I made this test program: public class Test { static List<Thread> al = new ArrayList<>(); public static void main(String[] args) throws IOException, InterruptedException { long startTime = System.currentTimeMillis(); al.add(new Thread(() -> fib1(47))); al.add(new Thread(() -> fib2(47))); for (Thread t : al) t.start(); for (Thread t: al) t.join(); long totalTime = System.currentTimeMillis() - startTime; System.out.println(totalTime); }

deadlock on synchronized ( String intern())

邮差的信 提交于 2019-12-22 04:48:10
问题 I user sun jdk 1.5 ThreadPoolExecutor( 24, 24,60,TimeUnit.SECONDS, new LinkedBlockingQueue()). soemtime I use jdb tool to find the status of all threads in thread pool are " waiting in a monitor", the code is : String key = getKey(dt.getPrefix(), id); synchronized (key.intern()) { -----> Is there a problem in "synchronized (key.intern()) " ? I get following informatnio using jdb tool, the status of 24 threads is "waiting in a monitor", it means 24 threads are deadlock at "key.intern()". (java

Why Volatile is behaving weirdly

回眸只為那壹抹淺笑 提交于 2019-12-22 04:37:20
问题 I have experience this weird behavior of volatile keyword recently. As far as i know, volatile keyword is applied on to the variable to reflect the changes done on the data of the variable by one thread onto the other thread. volatile keyword prevents caching of the data on the thread. I did a small test........ I used an integer variable named count, and used volatile keyword on it. Then made 2 different threads to increment the variable value to 10000, so the end resultant should be 20000.

线程之间的协作

空扰寡人 提交于 2019-12-22 03:38:00
引言 线程之间可以通过 wait() 、 notify() 和 notifyAll() 等方法,结合“生产者、消费者和队列”等设计模式来相互协作。线程之间协作的基础是 任务之间的握手 ,其基础特性是 互斥 。 wait()、notify()、notifyAll() 方法介绍: 以上三个方法都要获取对象监视器; wait() :挂起线程,并放弃当前对象监视器; notify() :唤醒一个等待‘notify()所在代码持有的对象监视器’的线程; notifyAll() :唤醒所有等待线程,并让他们竞争对象监视器; 其他方法 shutdown() : shutdownNow() : 一. wait() 和 notifyAll() 1.1基本特性 当任务遇到运行需要但是自身无法改变的条件时,可以调用 wait() 来等待这个条件发生变化,而且只有在 notify()、notifyAll() 发生时才会检查条件是否发生了变化。 注意,只有在同步代码块中才能使用 wait() 方法( notify() 方法同理),否则编译可以通过但是运行时会抛出 IllegalMonitorStateException 。 与 sleep()、yield() 不同的时: 在 wait() 期间锁是释放的,因此其他方法或代码块可以获得锁并执行; 可以通过 notify()、notifyAll()