The thread synchronization problem mostly uses the synchronized solves, has the synchronous code block and the synchronization method two kinds of ways, mainly remember these two kinds of difference
Test code:
1 PackageCom.xujingyang.testThread;2 3 Public classsynobj{4 Public synchronized voidShowA () {5System.out.println ("ShowA ...");6 Try {7Thread.Sleep (3000);8}Catch(interruptedexception e) {9 e.printstacktrace ();Ten } One } A - Public voidShowb () { - synchronized( This) { theSystem.out.println ("Showb ..."); - } - } - + Public voidSHOWC () { -String s= "1"; + synchronized(s) { ASystem.out.println ("SHOWC ..."); at } - } -}
PackageCom.xujingyang.testThread; Public classTest { Public Static voidMain (string[] args) {FinalSynobj sy=Newsynobj (); NewThread (NewRunnable () {@Override Public voidrun () {Sy.showa (); }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {SY.SHOWB (); }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {SY.SHOWC (); }}). Start (); }}
The results of the operation are as follows:
The printed result of this code is, SHOWA.....SHOWC ... will print out soon, showb .... will be printed out after a period of time, so why can't showb be called as soon as SHOWC?
After starting thread 1 calls method A, then the thread 1 sleeps for 3 seconds, then method C is called, notice that method C is locked here with synchronized, where the object of the lock is the string object of S. However, Method B is different, is to use the current object this to lock, notice that method a directly on the method to add synchronized, what is the lock object? Obviously, these two methods use a lock.
* By this result, we know how this synchronization method is locked, because the thread 1 in hibernation, then the lock is not released, resulting in thread 2 only after 3 seconds to call method B, thus, it is known that the two locking mechanism with the same lock object, that is, the current object.
In addition, the synchronous method directly on the method plus synchronized implementation lock, synchronous code block is locked inside the method, it is obvious that the synchronization method lock range is larger, and the synchronization code block range to small point, the general synchronization of the scope of the larger, the worse performance, generally need to lock to synchronize the time, Certainly the smaller the better, the better the performance *.
--------------------------------------------------------------------------------------------------------------- --------------------------------
First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.
Second, however, when a thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non-synchronized (this) synchronous code block in the object.
Third, in particular, when a thread accesses one synchronized (this) of an object to synchronize a block of code, other threads will block access to all other synchronized (this) synchronization blocks in object.
The difference between synchronization code blocks and synchronization methods for Java synchronized