Thread synchronization, that is, synchronizing the resources they handle to make their data valid. Synchronized, after using this keyword, any thread accesses, waits for the last thread to be completed before it can be executed. The only thing its declaration needs to care about is its scope, and there are generally two ways to declare it:
First: Add the Synchronized keyword in the declarative method
Public synchronized void Setxx () {}
The second type: synchronized block
Synchronized (syncobject) {
//control Code
}
The most fundamental difference between the two usages is in the scope of its scopes, below to illustrate a little
1: Add the Synchronized keyword in the declaration method, and its scope is implemented internally within this method. However, it corresponds to an instance of each class, that is, if two instances are declared in the same class, then the synchronized in the two classes are not conflicting.
public class Synchronizedtest {public
static void Main (String [] str) {
//Same instance
final dataclass DataClass = NE W DataClass ();
for (int i = 0; i < 1000 i++) {
final int num = i;
New Thread (New Runnable () {
@Override public
void Run () {
dataclass.setstr (num+ "");
}
). Start ();
}
Static class Dataclass{public
synchronized string setstr (String str) {
//destination print out 1:1~~~999:999
System.out.print (str);
System.out.print (":");
try {
thread.currentthread (). Sleep (m);
catch (Interruptedexception e) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
System.out.println (str);
return str;
}
}
However, if you put the above code
Final DataClass DataClass = new DataClass ();
Put inside the loop and declare a different instance, the output is disrupted
2:synchronized block, we'll change the statement above.
Public string Setstr (String str) {
synchronized (dataclass.class) {
//destination print out 1:1~~~999:999
System.out.print (str);
System.out.print (":");
try {
thread.currentthread (). Sleep (m);
catch (Interruptedexception e) {
//TODO auto-generated catch Block
e.printstacktrace ();
}
System.out.println (str);
return str;
}
}
This declarative way of extending the scope of the synchronized to the entire DataClass class, rather than to an instance, in the synchronized syntax block, we can fill in different scopes according to the requirements.
Another problem that we have to consider after thread synchronization is the communication between multiple threads, such as this one: there are now two threads, each output 10 messages per thread. Now, you want two threads to execute 10 times each and alternately. At this time, we may use the wait and the notify.
Wait: Set the thread to hibernate
Notify: Wake up the thread
public static void Main (String [] str) {//Same instance final DataClass DataClass = new DataClass (); New Thread (New Runnable () {@Override public void run () {for (int i = 0; i < i++) {Dataclass.threa
D1 ();
}}). Start ();
for (int i = 0; i < i++) {dataclass.thread2 ();
} Static Class Dataclass{public boolean Bo;
Public synchronized void Thread1 () {a try {while (!bo) {this.wait ();
for (int i = 0; i < i++) {System.out.println ("I am the 1th thread, looping" +i+ ")";
} Bo = false;
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{this.notify ();
} public synchronized void Thread2 () {a try {while () {this.wait ());
for (int i = 0; i < i++) {System.out.println ("I am the 2nd thread, looping" +i+ ")";
} Bo = true; catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktRace ();
}finally{this.notify (); }
}
}