synchronized

Cache flush on synchronized entry ,exit and volatile read and write

我们两清 提交于 2021-02-18 17:59:25
问题 When synchronized block execution is completed all the processor cache is flushed or only that object on which synchronized statement acted on will be flushed? In the below example when thread finished execution of method2 does data of obj2 is also flushed to the main memory? class cls1 { int x=10; } class cls2{ int y=10; } class cls3{ cls1 obj1; cls2 obj2; public void method2(){ obj2.y=10; synchronized(obj1){ obj1.x=30; } } public static void main(String[] args) { final cls3 obj3 = new cls3(

Do operations on ThreadLocal have to be synchronized?

百般思念 提交于 2021-02-07 12:52:47
问题 Here is the code I've stumbled across: class TransactionContextHolder { private static final ThreadLocal<TransactionContext> currentTransactionContext = new NamedInheritableThreadLocal<TransactionContext>( "Test Transaction Context"); static TransactionContext getCurrentTransactionContext() { return currentTransactionContext.get(); } static void setCurrentTransactionContext(TransactionContext transactionContext) { currentTransactionContext.set(transactionContext); } static TransactionContext

What does it mean when we say an ArrayList is not synchronized?

你。 提交于 2021-02-06 09:52:05
问题 What does it mean when we say an ArrayList is not synchronized? Does it mean that if we declare an ArrayList in object scope, multiple threads accessing the objects have the opportunity to modify the list? 回答1: What does it mean when we say an ArrayList is not synchronized? It means that accessing an ArrayList instance from multiple threads may not be safe (read, "may result in unexpected behavior" or "may not work as advertised"). Further reading: Synchronization and thread safety in Java

Same bytecode for method with or without synchronized keyword in method signature

痞子三分冷 提交于 2021-02-05 06:56:25
问题 For the following 2 classes got the same Java bytecode. java version: java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode) javac and javap version: 1.8.0_181 My doubt is shouldn't method with synchronized keyword have different bytecode as we can see in synchronized block has monitorenter and monitorexit , or let us assume I should not mix synchronized block and synchronized method then How does JVM

Scala Iterator for multithreading

余生颓废 提交于 2020-06-16 17:01:19
问题 I am using scala Iterator for waiting loop in synchronized block: anObject.synchronized { if (Try(anObject.foo()).isFailure) { Iterator.continually { anObject.wait() Try(anObject.foo()) }.dropWhile(_.isFailure).next() } anObject.notifyAll() } Is it acceptable to use Iterator with concurrency and multithreading? If not, why? And then what to use and how? There are some details, if it matters. anObject is a mutable queue. And there are multiple producers and consumers to the queue. So the block

do we need volatile when implementing singleton using double-check locking

懵懂的女人 提交于 2020-05-09 06:05:32
问题 suppose we use double-check lock to implement singleton pattern: private static Singleton instance; private static Object lock = new Object(); public static Singleton getInstance() { if(instance == null) { synchronized (lock) { if(instance == null) { instance = new Singleton(); } } } return instance; } Do we need to set variable "instance" as "volatile"? I hear a saying that we need it to disable reordering: When an object is created , reordering may happen: address=alloc instance=someAddress

do we need volatile when implementing singleton using double-check locking

馋奶兔 提交于 2020-05-09 06:04:21
问题 suppose we use double-check lock to implement singleton pattern: private static Singleton instance; private static Object lock = new Object(); public static Singleton getInstance() { if(instance == null) { synchronized (lock) { if(instance == null) { instance = new Singleton(); } } } return instance; } Do we need to set variable "instance" as "volatile"? I hear a saying that we need it to disable reordering: When an object is created , reordering may happen: address=alloc instance=someAddress

Java高级-线程同步机制实现

半腔热情 提交于 2020-04-15 05:48:30
【推荐阅读】微服务还能火多久?>>> 前言 我们可以在计算机上运行各种计算机软件程序。每一个运行的程序可能包括多个独立运行的线程(Thread)。 线程(Thread)是一份独立运行的程序,有自己专用的运行栈。线程有可能和其他线程共享一些资源,比如,内存,文件,数据库等。 当多个线程同时读写同一份共享资源的时候,可能会引起冲突。这时候,我们需要引入线程“同步”机制,即各位线程之间要有个先来后到,不能一窝蜂挤上去抢作一团。 同步这个词是从英文synchronize(使同时发生)翻译过来的。我也不明白为什么要用这个很容易引起误解的词。既然大家都这么用,咱们也就只好这么将就。 线程同步的真实意思和字面意思恰好相反。线程同步的真实意思,其实是“排队”:几个线程之间要排队,一个一个对共享资源进行操作,而不是同时进行操作。 关于线程同步,需要牢牢记住的第一点是 :线程同步就是线程排队。同步就是排队。线程同步的目的就是避免线程“同步”执行。这可真是个无聊的绕口令。 关于线程同步,需要牢牢记住的第二点是 :“共享”这两个字。只有共享资源的读写访问才需要同步。如果不是共享资源,那么就根本没有同步的必要。 关于线程同步,需要牢牢记住的第三点是 :只有“变量”才需要同步访问。如果共享的资源是固定不变的,那么就相当于“常量”,线程同时读取常量也不需要同步。至少一个线程修改共享资源,这样的情况下

jdk线程常见面试题

北慕城南 提交于 2020-04-08 12:26:37
请编写一个多线程程序,实现两个线程,其中一个线程完成对某个对象int成员变量的增加操作,即每次加1,另一个线程完成对该对象成员变量的减操作,即每次减1,同时要保证该变量的值不会小于0,不会大于1,该变量的初始值为0。 class Sample { private int number; public synchronized void increase() { while (0 != number) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number++; System.out.println(number); this.notifyAll(); } public synchronized void decrease() { while (0 == number) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number--; System.out.println(number); this.notifyAll(); } } class IncreaseThread implements Runnable { private Sample

synchronized

梦想与她 提交于 2020-04-07 23:14:16
概念 是利用锁的机制来实现同步的。 互斥性: 即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机制,这样在同一时间只有一个线程对需同步的代码块(复合操作)进行访问。互斥性我们也往往称为操作的原子性。 可见性: 必须确保在锁被释放之前,对共享变量所做的修改,对于随后获得该锁的另一个线程是可见的(即在获得锁时应获得最新共享变量的值),否则另一个线程可能是在本地缓存的某个副本上继续操作从而引起不一致。 用法 修饰静态方法: //同步静态方法 public synchronized static void methodName() { try { TimeUnit.SECONDS.sleep(2); System.out.println(Thread.currentThread().getName()+" aaa"); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { for (int i = 0; i < 5; i++) { new Thread(SynchronizedDemo::methodName).start(); } } 当synchronized作用于静态方法时,其锁就是当前类的class对象锁