Synchronized keywords
1) When a thread accesses an object's synchronized method or block of code, access to other threads will be blocked;
2) When a thread accesses an object's synchronized method or block of code, the other thread accesses the object's non-synchronized code block without blocking it;
3) When a thread accesses a synchronized method or block of code for an object, other threads will be blocked when they access other synchronized code blocks of that object.
First Rule
Public classTest { Public Static voidMain (String[]args) {//Create object for the threadsNewthread obj=NewNewthread (); //Create new ThreadThread thread1=NewThread (obj, "T1"); Thread thread2=NewThread (obj, "T2"); Thread1.start (); Thread2.start (); }}classNewthreadImplementsrunnable{@Override Public voidrun () {synchronized( This){ Try{ for(inti=0;i<3;i++) {Thread.Sleep (100); System.out.println (Thread.CurrentThread (). GetName ()+ ":" + (i+1)); } }Catch(interruptedexception e) {System.out.println ("Error"); } } } }
Rule number two:
Public classTest { Public Static voidMain (String[]args) {FinalDemo demo=NewDemo (); //Create first threadThread thread1=NewThread (NewRunnable () {@Override Public voidrun () {demo.synchronizedthread (); } },"T1"); //Create second threadThread thread2=NewThread (NewRunnable () {@Override Public voidrun () {demo.unsynchronizedthread (); } },"T2"); Thread1.start (); Thread2.start (); }}classdemo{//Synchronized Method Public voidSynchronizedthread () {synchronized( This){ Try{ for(inti=0;i<3;i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + (i+1)); Thread.Sleep (100); } }Catch(interruptedexception e) {System.out.println ("Error"); } } } //unsynchronized Method Public voidUnsynchronizedthread () {Try{ for(inti=0;i<3;i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + (i+1)); Thread.Sleep (100); } }Catch(interruptedexception e) {System.out.println ("Error"); } }}
Rule three:
Public classTest { Public Static voidMain (String[]args) {FinalDemo demo=NewDemo (); //Create first threadThread thread1=NewThread (NewRunnable () {@Override Public voidrun () {demo.synchronizedthread (); } },"T1"); //Create second threadThread thread2=NewThread (NewRunnable () {@Override Public voidrun () {demo.samesynchronizedthread (); } },"T2"); Thread1.start (); Thread2.start (); }}classdemo{//Synchronized Method Public voidSynchronizedthread () {synchronized( This){ Try{ for(inti=0;i<3;i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + (i+1)); //sleep (+)Thread.Sleep (100); } }Catch(interruptedexception e) {System.out.println ("Error"); } } } //Synchronized Method Public voidSamesynchronizedthread () {synchronized( This){ Try{ for(inti=0;i<3;i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + (i+1)); //sleep (+)Thread.Sleep (100); } }Catch(interruptedexception e) {System.out.println ("Error"); } } }}
Synchronized methods and blocks of code
Method: Public synchronized void Method1 () {}
Code block: Class Lei{synchronized (This)}
Reference from: HTTP://WWW.CNBLOGS.COM/SKYWANG12345/P/3479202.HTML#P3
Multithreading Diary (17.5.2two) basic rules for synchronized