Java multithreaded Learning Chapter (II) synchronized

Source: Internet
Author: User

There are two ways to modify synchronized:
    1. Decorate a method

       synchronized  public  void   runtest{ /**/} 
    2. Decorate a code block

       public  void   runtest{  Synchronized  (/*   An object or a class of  */ ) { /*  Span style= "COLOR: #008000" > code block  */ }} 
The scope of the synchronized is divided into the modification of a class and the modification of a total object
When you decorate an object , the same object calls the same code on different threads that can clog
When you decorate a class , the same class of different threads calls the same code, which can clog
Modifying a static method is equivalent to modifying a class

Define a class (used to verify the scope of the synchronized)

 Public classTestImplementsRunnable { Public Static intCount = 0; @Override Public voidrun () {runtest (); }     Public voidruntest () { for(inti = 0; I < 5; ++i) {Count++; System.out.println (Thread.CurrentThread (). GetName ()+ " " +Count); }    }}

When synchronized modifies a method

  1. If the method is a non-static method, the scope of the action is an object

    Clogging occurs when the method is called by the same object on different threads

    // Call the same object
    synchronized Public void runtest () { // modifier non-static method for (int i = 0; i < 5; + +i) { C12/>count++ ; + "" + Count);} }

    Call through the following code

    Newnew thread (test, "Thread_one"new thread (test, "Thread_two" ); thread _one.start (); Thread_two.start ();

    The result is

    Thread_one 123456789  10

    Because the call is the same test as the object in the two thread task, the first thread task runs and the second task is blocked

    If the object is not the same, no blockage will occur

    //Call two different objects New Thread (new test (), "Thread_one"new Thread (new test (), "Thread_two"), Thread_ One.start (); Thread_two.start ();

    The result is

    Thread_two 213456789  10

  2. When a modified method is a static method, the scope of the action is a class , not an object.

    Clogging occurs when the same class of different threads calls the method

    // call the same object New  New thread (test, "Thread_one"new thread (test, "Thread_two" ); thread_ One.start (); Thread_two.start (); //Call two different Objects New Thread (new test (), "Thread_one"new Thread (new test (), "Thread_two"  ); Thread_one.start (); Thread_two.start ();

    The results are

    Thread_one 12345678910

When synchronized modifies a block of code, the scope of the action looks at the contents in parentheses. if the object is in parentheses, the scope is an object, and if the parentheses are a class, the scope is a class

  1. If the object is in parentheses, the scope is an object, and the effect is the same as the synchronized adornment non-static method

     Public void runtest () {    synchronized (this// ) in parentheses is an object for         (  int i = 0; I < 5; + +i) {            Count+ +;             + "" + Count);     }}}

    Effect

    //call the same objectTest test =NewTest (); Thread Thread_one=NewThread (Test, "Thread_one") ); Thread Thread_two=NewThread (Test, "Thread_two")); Thread_one.start (); Thread_two.start ();//class clogging occurs//Thread_one 1//Thread_one 2//Thread_one 3//Thread_one 4//Thread_one 5//Thread_two 6//Thread_two 7//Thread_two 8//Thread_two 9//Thread_two Ten//call two different objectsThread Thread_one =NewThread (NewTest (), "Thread_one"); Thread Thread_two=NewThread (NewTest (), "Thread_two"); Thread_one.start (); Thread_two.start ();//no blockage occurred//Thread_two 2//Thread_one 1//Thread_two 3//Thread_one 4//Thread_two 5//Thread_one 6//Thread_two 7//Thread_one 8//Thread_two 9//Thread_one Ten
  2. If the bracket is a class, the scope is an object, and the effect is the same as the synchronized modifier static method

    Public [Static] void runtest () {      synchronized (Test.class//  in parentheses is a class for         ( int i = 0; I < 5; + +i) {            Count+ +;             + "" + Count);     }}}

    Two types of calls are blocked

    12345678910

Reference: http://tutorials.jenkov.com/java-concurrency/synchronized.html

Java multithreaded Learning Chapter (II) 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.