After implementing the Runnable interface, you can invoke the same piece of code resources through multiple start methods, which also leads to the problem that resources may not be synchronized.
The workaround for resource synchronization is to allow only one thread to manipulate the object or method during the same time period, and other threads to wait until the end of the thread's access to continue execution and lock it.
Keyword synchronized: Indicates that only one thread can execute a code block or method declared by synchronized, and other methods cannot lock the object during execution. There are synchronous code blocks and synchronous methods, both of which function the same way.
Only one synchronous method can access the object, and the other method cannot access the object if it is synchronized, but the non-synchronous method has access to the object (silly smacking)
Synchronizing code blocks
Public classTest { Public Static voidMain (string[] args) {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1")); Thread T2=NewThread (thread, "T2")); T1.start (); T2.start (); }}classMyThreadImplementsRunnable {Static intnum = 5; Public voidrun () {//synchronized (this) {//indicates that only one thread can execute this block of code and lock the code block below if(num > 0) { for(inti = 1;i < 6;i++) {num--; Try{Thread.Sleep (20);//acts as a magnifying effect}Catch(interruptedexception e) {System.out.println ("Thread hibernation Interrupted"); } System.out.println (Thread.CurrentThread (). GetName ()+ "Take away the remaining" +num); } } //} }}
After using the synchronization code block:
The synchronous method further explores a
Public classTest { Public Static voidMain (string[] args)throwsException {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1")); T1.start (); Thread.Sleep (10); THREAD.M2 (); }}classMyThreadImplementsRunnable {intnum = 1; Public synchronized voidRun () {//only means that the lock method cannot be executed by another thread at the same time and cannot be locked by another thread.num = 1000; Try{Thread.Sleep (5000); SYSTEM.OUT.PRINTLN (num); }Catch(interruptedexception e) {System.out.println ("Interrupted in hibernation"); } } voidm2 () {num= 2000; SYSTEM.OUT.PRINTLN (num); }}
Results
Discussion two
Public classTest { Public Static voidMain (string[] args)throwsException {MyThread thread=NewMyThread (); Thread T1=NewThread (thread, "T1")); T1.start (); Thread.Sleep (10); THREAD.M2 (); }}classMyThreadImplementsRunnable {intnum = 1; Public synchronized voidRun () {//only means that the lock method cannot be executed by another thread at the same time and cannot be locked by another thread.num = 1000; Try{Thread.Sleep (5000); SYSTEM.OUT.PRINTLN (num); }Catch(interruptedexception e) {System.out.println ("Interrupted in hibernation"); } } synchronized voidM2 () {//While other threads are running while the Run method is locked, the second lock on the object cannot benum = 2000; SYSTEM.OUT.PRINTLN (num); }}
Results
Java Learning Note Thread synchronization