"Multithreaded Programming" learning five: Synchronized synchronous statement block, static synchronization synchronized method and synchronized (class name. Classes) code block __java Multithreading

Source: Internet
Author: User

One, synchronized synchronous statement block:

Before introducing the synchronization statement block, do an experiment to verify that the order in which multiple threads call the same synchronization method is random.

Example one:

1) Mylist.java


2) Test:


It is visible that the code in the synchronization method is printed synchronously, but thread A and thread B are executed asynchronously, which may cause dirty reads. Look at the following example:


Example two: Create a set of only one element of the collection tool class, in the case of multithreading, "dirty Read", the collection has more than one element.

1 based on the above example, create the business class Mylistservice.java:


2 Test and Result:



Analysis: First list is empty, business class Mylistservice in the AddData () method is intended to ensure that the list can only put one element, but here appears "dirty read." When the first thread judge (List.getsize () < 1) is true, it enters the state of hibernation 2s, at which point the second thread judges

(List.getsize () <1) is also true, so it also sleeps 2s. This will add data when the two threads are asleep. We need to put if{...} Inside of the operation as a whole, so that no dirty reading will occur. You can then use a synchronized statement block to resolve the problem:


3 "synchronized (non-this object) synchronized code block" to solve the "dirty read" problem:


Repeat the above test, the results are as follows:


Visible, only one thread executes the Add () method, which implements synchronization between threads.

Summary:

1, whether the synchronized () method or the synchronized (object) {} code block, the effect is nothing more than a series of statements (curly braces around the statement) as a whole operation, at the same time only one thread can perform this whole operation.

2, synchronized keyword and object lock, execute synchronous method or synchronous statement block, must obtain the corresponding object lock first; If the corresponding object locks

has been obtained by a thread, the other threads must be freed for the corresponding object lock to execute the synchronization method or to synchronize the code block.


Two, static synchronous synchronized method and synchronized (class name. Classes) Statement block

1. Static synchronous Synchronized method

The keyword synchronized is added on static static methods to lock the class class corresponding to the current *.java file, and to the Non-static static method is to lock the object.

1 class file Service.java code is as follows:



2) Three thread classes Threada.java, Threadb.java, Threadc.java





3 Test one's code and result:


In the above example, two thread classes access the same service instance, but the result of "asynchronous" execution occurs because of the holding of different locks, one for class locks and the other for object locks.


4 test the code and result of two:


In the above test, two threads are accessing different instances of the same class, but all calls are static synchronization methods of the class, at which point the class lock works and two threads are synchronized.


2, synchronized (class name. Class) Statement block

The function of the synchronous synchronized (class name. Calss) code block is the same as that of the synchronized static method, changing the synchronized static to synchronized (Srevice.class) Do the related experiment, will get the same result, here no longer demonstrated.

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.