synchronized

volatile简介与原理

耗尽温柔 提交于 2019-12-03 09:47:24
一、计算机内存模型 1. CPU的高速缓存: a. 由于CPU的速度远远大于IO速度和主存速度,所以CPU加了一层高速缓存,把主存的数据加载到高速缓存 b. CPU高速缓存为某个CPU独有,只与运行在该CPU的线程有关 2. 缓存一致性问题: a. 当一个在主存里的变量被多个线程访问,成为共享变量,每个线程会把共享变量拷贝到当前线程的高速缓存,操作完后更新回主存 b. 可能一个线程操作的变量,被别的线程已经更新了,但是当前线程不知道 二、Java内存模型JMM 1、Java内存模型规定所有的变量都是存在主存当中(类似于前面说的物理内存),每个线程都有自己的工作内存(类似于前面的高速缓存) 2. 线程对变量的所有操作都必须在工作内存中进行,而不能直接对主存进行操作 3. 并且每个线程不能访问其他线程的工作内存 三、并发编程 1. 要想并发程序正确地执行,必须要保证原子性、可见性以及有序性,只要有一个没有被保证,就有可能会导致程序运行不正确 2. 原子性:一个操作要么不执行,要么全执行 3. 可见性:当一个线程修改了共享变量,其他线程立即可见 4. 有序性:代码执行是有先后顺序的,JVM会进行指令重排,指令重排不会影响单线程的操作结果,但有可能影响多线程的结果 四、volatile关键字 1. volatile关键字轻量级的synchronized,保证多线程正确执行 2. 可见性:

How does this recursive synchronized call not deadlock?

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a set of methods that all synchronize to the class object (can't use self, because multiple instances of this object could be used in multiple threads). Some of those methods call other methods in the class that also synchronize on the class object. Somehow this works and does not cause the deadlock I would expect it to. I would assume that testA would be blocked from running because testB already has a lock on the class object, but this apparently isn't the case. Is it something special that @synchronized is doing or is this a

Synchronized and the scope of visibility

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 threadTwo will be called

synchronize two threads in java

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have two threads in my java programme, one is main thread and other thread is thread A which is spawned in main thread. now i want main thread to start thread A and wait till thread A has executed some part of its code in run method and thread A should suspend itself. main thread should then start running, run few lines of code and then again thread A should start from where it has stopped and vice versa. this should happen for n number of times. I am trying as belows: Thread A class: public class ThreadA implements Runnable {

Proper use of volatile variables and synchronized blocks

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to wrap my head around thread safety in java (or in general). I have this class (which I hope complies with the definition of a POJO) which also needs to be compatible with JPA providers: public class SomeClass { private Object timestampLock = new Object(); // are "volatile"s necessary? private volatile java.sql.Timestamp timestamp; private volatile String timestampTimeZoneName; private volatile BigDecimal someValue; public ZonedDateTime getTimestamp() { // is synchronisation necessary here? is this the correct usage?

PHP pthreads - shared objects

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 no idea how to use it to get it working for my

Java: A synchronized method in the superclass acquires the same lock as one in the subclass, right?

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: class A { public synchronized void myOneMethod() { // ... } } class B extends A { public synchronized void myOtherMethod() { // ... } } // ... B myObject; // ... myObject.myOneMethod(); // acquires lock myObject.myOtherMethod(); // same lock? How I understand the synchronization model, I'd say that yes, it does, because the lock / monitor is associated with the instance myObject, and it doesn't matter where the method was defined. But am I right? If not, why? If yes, why are you sure, and I'm not? :-) 回答1: Yes, you are right, and you got the

NSMutableArray collection and @Synchronized blocks

假装没事ソ 提交于 2019-12-03 07:37:30
In Objective C I'm using a NSMutableArray instance from various thread and I'm using @synchronized to make it thread safe. currently all my acces to this array are protected with a @synchronized block, even objectAtIndex: method. Nevertheless I wonder which methods call really need to be protected with @synchronized. do I need to protect read access ? What would happens if 'ObjectAtIndex' is not protected and called at the same time that 'removeObject' ? If all methods are protected by a @synchronized what about performance? (I'writing a tcp/udp game server and really don't want to overprotect

Locks in Java(翻译blog)

别来无恙 提交于 2019-12-03 05:55:15
###翻译自 [Jakob Jenkov]( http://tutorials.jenkov.com/java-concurrency/locks.html#simple-lock from Java 5 the package java.util.concurrent.locks contains several lock implementations, so you may not have to implement your own locks. But you will still need to know how to use them, and it can still be useful to know the theory behind their implementation. For more details, see my tutorial on the java.util.concurrent.locks.Lock interface. //JAVA5以来,在java.util.concurrent.locks包中提供了若干种锁的实现,所以所以你可能不必实现你自己的锁。但是你需要知道如何使用它们,并且知道实现后面的原理。通过下面的代码块,看我关于锁的教程。 A Simple Lock Let's start out by looking at a

Intrinsic Locks and Synchronization

空扰寡人 提交于 2019-12-03 05:54:44
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock . (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility. Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the