Dark Horse Programmer:Java Training, Android training, iOS training,. NET Training
Java Threading-synchronized Detailed
I. Overview of synchronized
1, mutual exclusion between threads, must use the same monitor (an object)
2. The role of synchronized: Specifying a monitor for synchronous code blocks or synchronous methods
3. Using multiple blocks of code or multiple methods of the same monitor, at any moment, only the thread that gets the monitor can access a block of code or a method.
Second, Synchronized Action object
1. Synchronized statement BLOCK: You need to explicitly specify a monitor
1) Generate an object obj,synchronized (obj) {code block}
2) synchronized (this) {code block}
3) synchronized (Classname.class) {code block}
2. Synchronized method: Use the default monitor
1) instance method, the default monitor is: this
2) class method, the default monitor is: Classname.class (Class object)
Third, appears to use the same monitor, actually not
1. Use the object in the method body as a monitor
public void Method () {
Object obj = new Object ();
Synchronized (obj) {code block}
}
Cause: Each time the method is called, a new object, obj, is created, a new monitor, and the same monitor is not used between threads
Workaround: Use the instance member of the class, that is, "object obj = new object ();" Put it outside the method body
2. There are two issues to be aware of as a monitor using class instance members:
(1) The monitor changes when the object that the member points to changes. For example:
String str = new String ("123");
Public synchronized (str) void method1 () {...} Method1 using the Monitor "123"
str = new String ("456");
Public synchronized (str) void Method2 () {...} METHOD2 using the Monitor "456"
Cause: Using a different monitor, method1 and METHOD2 cannot be synchronized. If you change the direction of Str by means of the method, a confusing synchronization problem will occur.
Workaround: Use the final declaration as a class instance member of the monitor
(2) A monitor that uses class instance members as a class method is wrong: that is, instance members cannot be monitors for class methods.
3. Example Methods and class methods
Public synchronized void Objmethod () {...}
public static synchronized void Staticmethod () {...}
Cause: The monitor used by the instance method is: this, and the monitor used by the class method is: Classname.class
Workaround: Change the instance method to the following
Public synchronized (classname.class) void Objmethod () {...}
One problem: the child thread loops 10 times, then the main thread loops 20 times, so Loop 30 times
Technical points: 1, polling conditions
2. Wait () and notify () using object
Technical Requirements: 1, sub-thread task: Cycle 10 times
2. Main thread task: Cycle 20 times
3. The Sub-threading task is associated with the main thread task (running in turn) and should be placed in the same class (one of the encapsulation design principles of the Class)
4, sub-threading tasks and main thread tasks need to collaborate
5. Sub-threading tasks and main thread tasks executed 30 times
You can use the blocking queue design idea to modify the business code: Pay attention to the criteria
Class business{
Private Boolean issubschudule = Ture; Using Boolean variables for inter-threading collaboration
Public synchronized void sub (int loop) {
while (!issubschudule) {//Here you can use while to replace if to avoid false wakeup
try{
This.wait ();
}catch (...) {
.....
}
}
for (int i = 0; i < i++) {
System.out.println ("Sub:" + i + "loop:" + loop);
}
Issubschudule = false;
This.notify ();
}
Public synchronized void main (int loop) {
while (Issubschudule) {//Here you can use while to replace if to avoid false wakeup
try{
This.wait ();
}catch (...) {
.....
}
}
for (int i = 0; i <; i++) {
System.out.println ("Main::" + i + "loop:" + loop);
}
Issubschudule = ture;
This.notify ();
}
}
Main ()
Business business = new Business ();
Thread thread = new Thread (new Runnable () {
public void Run () {
for (int i = 0; i <; i++) {business.sub (i);}}). Start ();
for (int i = 0; i < i++) {//Add tasks to the main thread without implementing runnable
Business.main (i);
Dark Horse------Synchronized detailed