Java synchronized keywords

Source: Internet
Author: User

Java synchronized keywords

Synchronized is a keyword used in java multi-threaded programming to serialize operations between threads. This measure is similar to the use of exclusive locks in databases to implement concurrency control. However, the difference is that the database locks data objects, while java locks the code to be executed.

Note the following when using the synchronized keyword in java:

1. synchronized restricts access to the same resource. This is the reason for this keyword.


2. when a thread executes a code segment in a synchronized-modified class method or class method, the method or code segment is added with a mutex lock, at the same time, all the code segments in the class methods or class Methods Modified by synchronized are added with mutex lock, but it does not affect the code segments in the class methods or class methods that are not modified by synchronized, at the same time, it does not affect the thread to execute the code segments in the remaining class methods or class methods that have been locked and any methods in other classes; after a thread executes a code segment in a class method or class method modified by synchronized, these mutex locks will be released at the same time.


3. For an object, when a thread executes a code segment in a synchronized modified object method or object method, the situation is the same as that in 2.


4. There is no impact between points 3rd and 2nd.


5. The content in the synchronized () brackets indicates whether the resource to be blocked is a class or a method or code block in an object. Because synchronized modifiers cannot be used in the class declaration, you must use synchronized (ClassName. class ){/*... */} form or use synchronized to modify the Class Method

 

The instance code is as follows:

(1) Printer class

1 public class Printer {2 public static void printA () 3 {4 System. out. println (Thread. currentThread () + ": invoke printA method"); 5 synchronized (Printer. class) 6 {7 for (int I = 0; I <5; I ++) 8 {9 System. out. println (Thread. currentThread () + ": into printA method's loop... "); 10 try11 {12 Thread. sleep (1000); 13} 14 catch (InterruptedException e) 15 {16 e. printStackTrace (); 17} 18} 19} 20} 21 22 public synchronized void printB () 23 {24 System. out. println (Thread. currentThread () + ": invoke printB method"); 25 for (int I = 0; I <5; I ++) 26 {27 System. out. println (Thread. currentThread () + ": into printB method's loop... "); 28 try29 {30 Thread. sleep (1000); 31} 32 catch (InterruptedException e) 33 {34 e. printStackTrace (); 35} 36} 37} 38}View Code

(2) ThreadA class

1 package demo; 2 3 public class ThreadA extends Thread {4 Printer p = null; 5 public ThreadA (Printer p) 6 {7 super ("ThreadA"); 8 this. p = p; 9} 10 public void run () 11 {12 // p. printA (); 13 Printer. printA (); 14} 15}View Code

(3) ThreadB class

1 package demo; 2 3 public class ThreadB extends Thread {4 Printer p = null; 5 public ThreadB (Printer p) 6 {7 super ("ThreadB"); 8 this. p = p; 9} 10 public void run () 11 {12 // p. printB (); 13 Printer. printB (); 14} 15}View Code

(4) MainThread class

1 package demo; 2 3 public class MainThread {4 public static void main (String [] args) 5 {6 Printer p = new Printer (); 7 ThreadA ta = new ThreadA (p); 8 ThreadB tb = new ThreadB (p); 9 10 ta. start (); 11 try {12 ta. join (10); 13} catch (InterruptedException e) {14 // TODO Auto-generated catch block15 e. printStackTrace (); 16} 17 tb. start (); 18} 19}View Code

The running result is as follows:

Thread [ThreadA, 5, main]: invoke printA method
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadB, 5, main]: invoke printB method
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...

From the above results, we can see that ThreadA first calls the run method and enters the for loop block of the printA method. Therefore, the mutex lock is obtained first. According to 2, we can see that ThreadB cannot execute the printB method. Add join (10) To MainThread) to give ThreadA more time so that it can first enter the printA method (this is because ThreadA needs to obtain the mutex lock after executing a pirnt Statement ). If the join statement is removed, this point can be verified.

Add a printC Method to the Printer class:

1 public void printC () 2 {3 System. out. println (Thread. currentThread () + ": invoke printC method"); 4}View Code

Create a ThreadC class:

1 package demo; 2 3 public class ThreadC extends Thread {4 Printer p = null; 5 public ThreadC (Printer p) 6 {7 this. p = p; 8} 9 public void run () 10 {11 for (int I = 0; I <5; I ++) 12 {13 p. printC (); 14} 15} 16}View Code

Then remove the join statement from MainThread and activate the ThreadC thread. The running result is as follows (the test result may be different ):

Thread [Thread-0, 5, main]: invoke printC method
Thread [ThreadA, 5, main]: invoke printA method
Thread [Thread-0, 5, main]: invoke printC method
Thread [Thread-0, 5, main]: invoke printC method
Thread [ThreadB, 5, main]: invoke printB method
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [Thread-0, 5, main]: invoke printC method
Thread [Thread-0, 5, main]: invoke printC method
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...

For the above results, you can use 2 for your own explanation. You can also test and verify the remaining points in 2 and the situation of objects. Verification 4 is shown below.

Remove the static keyword in the printB method declaration in the Printer method, and replace Printer. printB () with p. printB () in the ThreadB class run method. The running result is as follows:

Thread [ThreadA, 5, main]: invoke printA method
Thread [ThreadB, 5, main]: invoke printB method
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [Thread-0, 5, main]: invoke printC method
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [Thread-0, 5, main]: invoke printC method
Thread [Thread-0, 5, main]: invoke printC method
Thread [Thread-0, 5, main]: invoke printC method
Thread [Thread-0, 5, main]: invoke printC method
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...
Thread [ThreadB, 5, main]: into printB method's loop...
Thread [ThreadA, 5, main]: into printA method's loop...

You can analyze the output results carefully.

 

(Not complete To be continued ...; -))

For more information, see Devin Zhang's blog.

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.