Article Annotated Source: Http://blog.csdn.net/yinwenjie, mainly for their own study reproduced with
1. The status of threads in Java
1-1. #对象锁
-
Every object in Java has a ' lock ', which can be open, or it can hold a ' key ' by one thread (or multiple threads); once the ' lock ' of an object in the system has at least one ' key ' , Then any thread that operates on this object must verify that there is a ' key ', and if not, it will report an illegalmonitorstateexception exception.
-
but ' lock core ' (object exclusive rights) there is only one , you can open this lock of multiple ' keys ' at the same time can only have a ' key ' to operate; others hold ' The key ' thread (or a thread that does not have a key) is going to wait until a key is exited from the keyhole, and the operating system decides which key to reinsert into the ' lock ' (OS thread switch).
-
A thread can "lock" any object at any time during execution, or it can return the ' key ' of the object lock at any time. If the ' key ' of the object lock is returned, then the thread (in which the object is locked) must no longer have the right to operate the object. The
-
Needs to be aware that the ' key ' of a thread that has a ' lock ' on an object does not mean that the ' key ' of the thread is inserting a ' lock ' (with control of the object); However, the right to preempt ' Lock the core ' control of the thread, must have the object's ' key '. In the
-
Thread, the ' lock core ' represents the exclusive control of the object, and the keyword that requires detection of the lock state of an object is ' synchronized ' (The critical section). Can pull the key out of the lock, but do not return the lock call method (Release the object exclusive state, and can be in the subsequent participation of the object Exclusive competition method), only wait and wait (time). The way to return keys includes: The critical section of a thread that is normal for an object is completed normally, and an exception is thrown out of the critical section (critical section Completion)
Be sure to pay attention to the use of wait (time). Many beginners (including myself for a long time) understand that ' wait for a time and then the thread activates to continue working ', but if you follow the above description, you will find that the understanding is problematic. In fact time more accurate meaning should be: to check. The entire wait time is more accurately understood to be: Release the lock core (exclusive) of this thread's exclusive X object, so that other threads that can preempt the ' lock ' (exclusive) can preempt, but this thread continues to hold the key to the lock of the X object, and after a while, to re-participate in the ' Lock Core ' preemption (not necessarily be able to preempt).
1-2. The working process of object lock
Represents the working process of an object lock in a thread:
Let's look at the working process of the lock described in a practical code example:
1 PackageTest.thread.lock;2 3 ImportOrg.apache.commons.logging.Log;4 Importorg.apache.commons.logging.LogFactory;5 6 Public classThreadlock {7 8 /**9 * To lock the objectTen */ One Private Static FinalObject Wait_object =NewObject (); A - /** - * Log, if you do not have log4j, please use System.out the */ - Private Static FinalLog LOGGER = Logfactory.getlog (threadlock.class); - - /** + * Lazy, I threw out all the exceptions. Formal system don't do this!! - * @paramargs + * @throwsException A */ at Public Static voidMain (string[] args)throwsException { -Thread Threada =NewThread (NewRunnable () { - @Override - Public voidrun () { - //Check the ' object lock ' status. - synchronized(threadlock.wait_object) { inThreadLock.LOGGER.info ("Do some things ....") "); - } to } + }); - theThread threadb =NewThread (NewRunnable () { * @Override $ Public voidrun () {Panax Notoginseng //Check the ' object lock ' status. - synchronized(threadlock.wait_object) { theThreadLock.LOGGER.info ("Do something else ....") "); + } A } the }); + Threada.start (); - Threadb.start (); $ } $}
The code is simple and there should be no need to do too much: Create two threads Threada and threadb. The object lock state Check for Threadlock.wait_object is required for the execution method of two threads. Let's look at the thread execution through the debug state of Eclipse:
-
Threada and THREADB began to work. Note that this is the key for two threads that do not hold any object locks:
-
Threada begins the Threadlock.wait_object object lock check and discovers that no thread holds the lock core for this object lock. The JVM then creates an object lock key for this thread and makes the key of the thread occupy the lock core (exclusive of the object's Operation rights):
-
At this point Threadb also began to Threadlock.wait_object object lock check, but found that Threadlock.wait_object object lock lock Core has been occupied by a thread (Threada is exclusive), so stay in the check bit wait:
-
Threada continues to run until the critical section of the Synchronized keyword is fully executed, releasing the lock core and returning the lock. Threadb found that Threadlock.wait_object is no longer in exclusive state (lock core without any key to where), so the waiting state, get Threadlock.wait_object object lock key, insert the lock core, continue to execute:
1-3, the synchronized can be labeled position
The Synchronized keyword in Java can be loaded in many places. You can add synchronized keywords to a method definition, add synchronized keywords to the method body, and add synchronized keywords to the static block. The following code is correct:
1 //Code Snippet 12 Static {3 synchronized(Threadlock.class) {4 5 }6 }7 8 //Code Snippet 29 Public synchronized voidSomeMethod () {Ten One } A - //Code Snippet 3 - Public synchronized Static voidSomeMethod () { the - } - - //Code Snippet 4 + Public Static voidSomeMethod () { - synchronized(Threadlock.class) { + A } at } - - //Code Snippet 5 - Public voidSomeMethod () { - synchronized(Threadlock.class) { - in } -}
But the synchronized keyword in different positions means that the meaning is not the same. Synchronized () {}, the developer can specify the object lock to be checked. But when synchronized is loaded on the method, some readers feel a bit confused. Here is a detailed explanation:
-
When the Synchronized keyword is loaded on a non-static method: it represents the same meaning as synchronized (this) {}. That is, the object that owns this method is checked for object lock state.
-
When the Synchronized keyword is loaded on a static method: the meaning of its representation is similar to that of synchronized (Class.class). That is, the object of the class that owns the method is checked for object lock state (the class itself is also an object, OH ^_^).
Thread Base: Thread (2) basic threading operations in--java (top)