synchronized

可重入锁与不可重入锁之间的区别与性能差异?

岁酱吖の 提交于 2019-12-06 05:25:22
可重入锁 指在同一个线程在外层方法获取锁的时候,进入内层方法会自动获取锁。 为了避免死锁的发生,JDK 中基本都是可重入锁。 下面我们来测试一下 synchronized 和 java.util.concurrent.lock.ReentrantLock 锁的可重入性 测试 synchronized 加锁 可重入性 package constxiong.concurrency.a019; /** * 测试 synchronized 加锁 可重入性 * @author ConstXiong * @date 2019-09-20 15:55:27 */ public class TestSynchronizedReentrant { public static void main(String[] args) { new Thread(new SynchronizedReentrant()).start(); } } class SynchronizedReentrant implements Runnable { private final Object obj = new Object(); /** * 方法1,调用方法2 */ public void method1() { synchronized (obj) { System.out.println(Thread

Acquiring multiple locks atomically in java

空扰寡人 提交于 2019-12-06 05:06:31
问题 I have the following code: Note: I simplified the code as much as possible for readability. If I forgot any critical pieces let me know. public class User(){ private Relations relations; public User(){ relations = new Relations(this); } public getRelations(){ return relations; } } public class Relations(){ private User user; public Relations(User user){ this.user = user; } public synchronized void setRelation(User user2){ Relations relations2 = user2.getRelations(); synchronized(relations2){

并发编程—Volatile关键字

好久不见. 提交于 2019-12-06 03:42:34
锁提供了两种主要特性:互斥(mutual exclusion) 和可见性(visibility)。互斥即一次只允许一个线程持有某个特定的锁,因此可以保证一次就只有一个线程在访问共享数据。可见性要复杂一些,它必须确保释放锁之前对共享数据做出的更改对于随后获得该锁的另一个线程是可见的。 volatile 变量可以被看作是一种 “轻量级的 synchronized”,与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但是它所能实现的功能也仅是 synchronized 的一部分。 volatile变量 一个共享变量被volatile修饰之后,则具有了两层语义: 保证了该变量在多个线程的可见性(这是结果) 会通过Lock命令禁止特定类型的命令重排序(这是原因) volatile的实现原理 首先了解一下内存屏障(memory barriers)的概念,内存屏障是一组处理器命令,用于实现对内存操作的顺序限制。 如果代码中的共享变量被volatile修饰,在生成汇编代码时会在volatile修饰的共享变量进行写操作的时候会多出Lock前缀的命令。在多核处理器的情况下,这个Lock命令主要有3个功能: volatile的变量被修改后会立即写入到主存中 这个写回主存的操作会告知其它线程中该变量对应的缓存行失效,所以其它线程如果要操作这个变量

多线程学习笔记(十二)

放肆的年华 提交于 2019-12-06 03:31:03
volatile的作用是使变量在多个线程间可见 1.死循环 public class PrintInfo implements Runnable { private boolean statu = true; public boolean isStatu() { return statu; } public void setStatu(boolean statu) { this.statu = statu; } public void printInfo() { try { while(statu == true){ System.out.println("The Thread Name Is " + Thread.currentThread().getName()); Thread.sleep(1000); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } } @Override public void run() { printInfo(); } } public class Run { public static void main(String[] args){ PrintInfo printInfo = new PrintInfo(); printInfo.printInfo();

三个线程循环打印ABC10次的几种解决方法

≡放荡痞女 提交于 2019-12-06 03:18:05
有三个线程分别打印A、B、C, 请用多线程编程实现,在屏幕上循环打印10次ABCABC… 这是一个比较常用的关于线程的考题,一般出现在应届生的校园招聘试卷上。 本文给出如下四种解决方法: 使用synchronized, wait和notifyAll 使用Lock 和 Condition 使用Semaphore 使用AtomicInteger 使用synchronized, wait和notifyAll /** * @author wangmengjun * */ public class SyncObj { private char letter = 'A'; public void nextLetter() { switch (letter) { case 'A': letter = 'B'; break; case 'B': letter = 'C'; break; case 'C': letter = 'A'; break; default: break; } } public char getLetter() { return letter; } } /** * @author wangmengjun * */ public class PrintLetterRunnable implements Runnable { private SyncObj syncObj;

Thread Synchronization in Django

风格不统一 提交于 2019-12-06 03:10:57
问题 Is there any way to block a critical area like with Java synchronized in Django? 回答1: You can use locks to make sure that only one Thread will access a certain block of code at a time. To do this, you simply create a Lock object then acquire the lock before the block of code you want to synchronize. An example: lock = Lock() lock.acquire() # will block if another thread has lock try: ... use lock finally: lock.release() For more information , see http://effbot.org/zone/thread-synchronization

How do I check if an object is being @synchronized

不问归期 提交于 2019-12-06 03:07:08
问题 Sometimes I wrote the following code to synchronized a routine: @synchronized(objToBeSync){ .... } When two threads try to access the sync block at the same time, one will block the others, until the one exits the sync block. However, sometimes I don't want one blocks the other, but the others check if the object is being synchronized, and then do some other thing so I have to do sth like this: @synchronized(objToBeSync){ _isBeingSync = YES; ... _isBeingSync = NO; } _isBeingSync is an

Mutually exclusive method execution in Java

ぃ、小莉子 提交于 2019-12-06 02:58:44
问题 I have two methods: a() and b() . While I'm fine with multiple threads accessing any of the methods at the same time (that is desirable), I do not want any threads to enter a() while b() is being executed. How do I do that? Edit 1 Lets say there are 4 threads and Thread 1 is accessing A() . What I want is all of the 4 threads should not use B() . 回答1: CHECK UPDATE at the bottom - I don't think this approach can work. Leaving it for information. You could use a Semaphore: if a thread is in b()

多线程学习笔记(八)

大城市里の小女人 提交于 2019-12-06 02:03:40
注: 当一个线程执行的代码出现异常时,其所持有的锁会自动释放 同步不具有继承性 不在synchronized块中就是一部执行,在synchronized中就是同步执行 和synchronized方法一样,synchronized(this)代码块也是锁定当前对象的 1. 同步的弊端 public class CommonUtils { public static long startTime1; public static long endTime1; public static long startTime2; public static long endTime2; } public class DemeritOfSynchronized { private String username; private String password; public synchronized void test() { try{ System.out.println("begin the test"); Thread.sleep(3000); username = "username is " + Thread.currentThread().getName(); password = "password is " + System.currentTimeMillis(); System

使用synchronized来保证数据的同步

China☆狼群 提交于 2019-12-06 02:03:30
网上一搜索synchronized,资料大把大把,所以也不展开来说,怕说多错多。简单说说我的理解和用法。 synchronized是Java中的关键字,是一种同步锁。通常可以用来修饰某个方法,或者某个代码块。 我一般用来修饰代码块,感觉会更加灵活。先上个例子: public class Task implements Runnable{ private static final Object LOCK = new Object(); public void run(){ long threadId = Thread.currentThread().getId(); System.out.println(threadId + " 等待执行"); synchronized (LOCK) { try { System.out.println(threadId + " 执行中"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(threadId + " 执行结束"); } } } 首先我们定义了一个常量LOCK,执行到这个synchronized 代码块的时候,都会先检查是否获得LOCK的使用权。 由于LOCK是全局的常量