Many students write this code is easy to write confused. In fact, as long as the following steps of analysis, you will find that the problem is very simple.
1 clearing cycle 50 times and child threads run 10 times, main thread run 100 times, don't cross together
A loop of 50 times can be considered a caller action, the caller can choose to operate 50 times, or you can choose to operate 100 times.
The child thread and the main thread can be treated as the callee interface, and as soon as the caller calls me, I execute the xxx time (the child thread runs 10 times and the main thread runs 100 times).
2 Note one point:
The primary thread is not allowed to run until the first 10 completion of child threads is run, and the child thread is not allowed to run the second 10 operations. Example: When the caller first calls, the first 10 calls of the child thread run to the 8th time. When the child thread holds the lock, the main thread is not coming in, but the child thread is allowed in, which causes the caller to invoke the child thread to run the second 10 times without processing, the first 10 runs of the child thread and the second 10 run, and how to deal with it. The answer is thread communication. You can use Wait (), notify (), or use java.util.concurrent.locks.Condition.
With the above clear thinking code is good to write.
3 Re-entry lock
Why is the first 10 run of the child thread mixed with the second 10 times run. Why the first 10-run lock can still be executed the second 10 times.
Synchronized can be reentrant
An synchronized method/block that calls the other Synchronized method/block in its own class or calls the parent class does not hinder execution of that thread. That is, the same thread on the same object lock can be reentrant, and the same threads can get the same lock multiple times, that is, can be repeated multiple times. Click on the Open link.
re-entry lock
(1) Re-entry:
1. definition : Re-entry means that any thread acquires the lock again after acquiring it without being blocked by the lock. Associating a thread holder + counter, reentrant means that the granularity of the lock operation is "thread".
2. two issues need to be addressed :
The thread acquires the lock again : The lock needs to identify whether the field acquiring the lock is the thread that currently occupies the lock, and if so, succeeds again;
final release of the lock : the thread repeats the lock again n times, and then the lock is released by the other thread after the first n release. Requirements on the lock for the acquisition of the number of times, the counter to the current number of times the lock is repeatedly obtained statistics, when the lock is released, the counter from the reduction, when the counter value of 0 o'clock, the lock successfully released.
3. Re-entry Lock implementation reentrant: Each lock is associated with a thread holder and counter , and when the counter is 0 that the lock is not held by any thread, then any thread may acquire the lock and invoke the corresponding method; The JVM notes the lock's holding thread and resets the counter to 1; when another thread requests the lock, it must wait, and the thread that holds the lock can get the lock again if it is requested again, while the counter increments, and when the thread exits the synchronized code block, the counter is decremented and the lock is released if the counter is 0
Next article: http://blog.csdn.net/wabiaozia/article/details/79534560