Synchronized modification method of JAVA synchronization

Source: Internet
Author: User
Tags tag name

In Java multithreaded programming, the code that needs to execute concurrently is placed inside the run method of the thread class, and then the object of the thread class is created, and the start () method is called to start execution.

When a piece of code needs to be mutually exclusive, it can be decorated with the Synchronized keyword, which discusses how the Synchronized keyword decoration method is mutually exclusive.

The synchronized modifies the method when the object that called the method is locked. It does not allow multiple objects that call the method to be mutually exclusive in the order in which they are executed.

Here is a specific example:

Test.java becomes a thread class through the implements Runnable, which has a Methodsync instance variable so that a corresponding Methodsync object is initialized whenever a test object is instantiated (equivalent to creating a thread). It then calls the synchronized-modified method inside the run () method.

Test.java

1  Public classTestImplementsrunnable{2     PrivateString name;3 //private static Methodsync Methodsync = new Methodsync ();4     PrivateMethodsync Methodsync =NewMethodsync ();5     6      PublicTest (String name) {7          This. Name =name;8     }9     Ten @Override One      Public voidrun () { A Methodsync.method (name); -     } -      the      Public Static voidMain (string[] args) { -Thread T1 =NewThread (NewTest ("Test 1")); -Thread t2 =NewThread (NewTest ("Test 2")); - T1.start (); + T2.start (); -     } +}

Methodsync.java, the class has only one synchronized method for testing

1  Public classMethodsync {2     3     /*4 * @Task: The object that called the method is locked when testing the synchronized adornment method5 * @param the tag name of the name thread6      */7      Public synchronized voidmethod (String name) {8SYSTEM.OUT.PRINTLN (name + "Start a Sync Method"));9         Try{TenThread.Sleep (300); One}Catch(Interruptedexception e) {} ASYSTEM.OUT.PRINTLN (name + "End of the Sync Method")); -     } -}

First look at the execution results:

Test1 executes the synchronization method before Test2, but ends at Test2. There is no mutually exclusive effect! The reason is that Methodsync is an instance variable that creates a Methodsync object each time a test object is created, and synchronized only locks the Methodsync object that calls the method () methods. The two threads created here have two different Methodsync objects, and they do not have a mutex when they call method methods.

When the Methodsync variable in the Test.java is modified with static, the result is as follows:

Here, the synchronization function is realized correctly. The reasons are as follows: Two threads (Test objects) are created here, but each test object shares the Methodsync variable, which means that only one Methodsync variable executes the method in two threads, so that two threads execute method, the code is mutually exclusive.

Synchronized modification method of JAVA synchronization

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.