Multithreading Diary (17.5.2two) basic rules for synchronized

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.