Every object in Java has an object lock, and synchronized is going to get this lock, see the example below
ImportJava.util.Random; Public classmydata{ Public synchronized voidincrement () { for(inti = 0; I < 10; i++) { Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); } } Public synchronized voidDecrement () { for(inti = 0; I < 10; i++) { Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); } } Public Static voidMain (string[] args) {FinalMyData myData1 =NewMyData (); //final MyData myData2 = new MyData (); NewThread (NewRunnable () {@Override Public voidrun () {mydata1.increment (); }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {mydata1.decrement (); }}). Start (); }}
No matter how many times the execution is ordered, two threads operate on the same object, the first executing thread gets a lock, and the second thread can only wait until the first thread finishes executing to get the lock and enter the method.
Let's look at the following example
ImportJava.util.Random; Public classmydata{ Public synchronized voidincrement () { for(inti = 0; I < 10; i++) { Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); } } Public synchronized voidDecrement () { for(inti = 0; I < 10; i++) { Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); } } Public Static voidMain (string[] args) {FinalMyData myData1 =NewMyData (); FinalMyData MyData2 =NewMyData (); NewThread (NewRunnable () {@Override Public voidrun () {mydata1.increment (); }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {mydata2.decrement (); }}). Start (); }}
The result of execution is unordered, two objects, two locks, so they do not affect each other, each executes their own.
Let's take a look at the following example
Import Java.util.Random;
public class mydata{
public synchronized void increment () {
for (int i = 0; i < i++) {
try {
Thread.Sleep (New Random (). Nextint (200));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
public static synchronized void decrement () {
for (int i = 0; i < i++) {
try {
Thread.Sleep (New Random (). Nextint (200));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}
public static void Main (string[] args) {
Final MyData myData1 = new MyData ();
Final MyData myData2 = new MyData ();
New Thread (New Runnable () {
@Override
public void Run () {
Mydata1.increment ();
}
}). Start ();
New Thread (New Runnable () {
@Override
public void Run () {
Mydata.decrement ();
}
}). Start ();
}
}
The result is also unordered, the same reason as above, the static method belongs to the class object, so the decrement method locks the Mydata.class object, and mydata1.increment (); The lock is a MyData1 object that does not interfere with each other.
Just remember that the synchronized lock is an object, each object has an object lock, to get the lock to execute synchronized method
Synchronized keywords in Java