Java multithreaded 5:synchronized Lock method block

Source: Internet
Author: User

Synchronized synchronizing code blocks

Using the keyword Synchronized Declaration method in some cases is a disadvantage, such as a thread call synchronous method to perform a longer task, then the B thread must wait for a long time. In this case, you can try to use the Synchronized synchronization statement block to resolve the problem. Take a look at the example:

The following two conclusions can be drawn from this experiment:

1, when a thread accesses the object's synchronized code block, the B thread can still access the rest of the non-synchronized blocks in the object method, and the first part of the execution results proves this

2, when a thread enters the object's synchronized code block, if the B thread wants to access this block of synchronized, then the access will be blocked, and the second part of the execution results proves this

So, from the point of view of execution efficiency, sometimes we may not have to add the whole method to synchronized, but can take the synchronized block of the way, will cause the thread security problem that part of the code to synchronized on it.

The results of the operation are divided into two parts:

Two synchronized blocks are mutually exclusive

If thread 1 accesses the synchronized block of an object a method, then thread B's access to the synchronized block of the same object B method is blocked, and an example is written to prove it:

You see that access to the Servicemethodb () method synchronized block must wait until the end of the access to the synchronized block for the Servicemethoda () method. In this case, we can also conclude thatthe synchronized block obtains an object lock, in other words, the synchronized block locks the entire object .

Synchronized block and Synchronized method

Now that we've got a conclusion synchronized block gets an object lock , if thread 1 accesses an object method A's synchronized block, thread 2 's access to the same object's synchronous method B should be blocked. Because thread 2 accesses the same object's synchronous method B, it will attempt to get the object lock of the object, but this lock is thread 1 here. Write an example to prove the conclusion:

To prove our conclusion. To further refine this conclusion, remove the synchronized of the "Othermethod ()" Method and look at the results of the operation:

The "Othermethod ()" Method and the synchronized block in the "Dolongtask ()" Method are executed asynchronously

To use any object as an object monitor

Summarize the previous content:

1. Synchronized synchronization method

(1) Blocking state for other synchronized synchronization methods or synchronized (this) synchronization code block

(2) Only one thread can execute the code in the synchronized synchronization method at the same time

2. Synchronized Synchronous code block

(1) Blocking state for other synchronized synchronization methods or synchronized (this) synchronization code block

(2) Only one thread at a time can execute code in the synchronized (this) synchronous code block

The synchronized (this) format is used before to synchronize blocks of code, but Java also supports the ability to synchronize "arbitrary objects" as object monitors . Most of the "arbitrary objects" are arguments to the instance variables and methods , using the format synchronized (not the This object). Take a look at examples of using arbitrary objects as object monitors:

This example proves that multiple threads holding the object monitor as the same object can only have one thread executing the code in the synchronized (not the This object x) code block at a time.

The lock non-this object has some advantages: if there are many synchronized methods in a class, the synchronization can be achieved, but it will be blocked, thus affecting the efficiency. However, if the synchronization code block lock is a non-this object, then the program in the synchronized (non-this object x) code block is asynchronous and does not compete with the other lock this synchronous method for this lock, which greatly improves the operational efficiency.

Notice "private String anystring = new string ();" This sentence, now it is a global object, so the same object is being monitored. If you move to a try, then the object's monitor is not the same, the call is naturally asynchronous call, you can try it yourself.

Finally, synchronized (not this object x), if this object is an instance variable, refers to the object reference, as long as the object's reference is not changed, even if the object's properties are changed, the running result is still synchronous .

Three conclusions for thinning synchronized (not this object x)

The notation for synchronized (not this object x) format is to use the X object itself as an object monitor, with three conclusions:

1. Synchronized when multiple threads are executing synchronized (x) {} Simultaneously while synchronizing code blocks

2. Synchronous effect when other threads execute the synchronized synchronization method in X object

3. Synchronous effect when other threads execute the synchronized (this) block in the X object method

1th, it is obvious that the 2nd and 3rd meanings are similar, nothing more than a synchronous method, a synchronous block of code, and an example to verify the 2nd:

Write two threads to call the "speedprintstring ()" Method and the "TESTMETHOD1 (MyObject mo)" method respectively:

See the "speedprintstring ()" Method must wait for the "testMethod1 (MyObject mo)" method to execute before execution, there is no way to execute asynchronously, to prove the 2nd conclusion. The 3rd verification method is similar, it does not write code proof.

Java multithreaded 5:synchronized Lock method block

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.