Multi-thread communication Lock locks

According to jdk description of the Lock interface Lockimplementations provide than using synchronizeda wider range of locking methods of operation and statements available. They allow more flexible structuring, may have completely different properties, and may support a plurality of associated with the objects Condition.

Java is a synchronized keyword, and Lock is an interface. Lock Lock is a much more flexible than the synchronized keyword lock thread communication, synchronized locks wait (blocking the thread releases the lock) and notisfy (random wake up one thread) and notisfyAll (wake up all the threads) in three-thread communication in methods. Thread communication can not be specified in the wake of a thread, all threads can only be waking judge, and then decide which threads to run down, so that in many cases will result in a waste of resources, the formation of unnecessary overhead. The api lock lock can be specified separately provide a thread wakes up, so in the case of provisions for the implementation of the process thread, more flexible and practical than using the synchronized keyword.

For example the following case: three threads, one thread required print number five 1-5, 1-10 after thread 2 print number ten, fifteen threads 3 1-15 print number, a print number 5 after thread, the thread 10 second print number. . . . . . Cycle 5 cycles.

 

Methods Lock locks the thread of communication:

 

 1 import java.util.concurrent.locks.Condition;
 2 import java.util.concurrent.locks.Lock;
 3 import java.util.concurrent.locks.ReentrantLock;
 4 
 5 public class Print_01 {
 6     public static void main(String[] args) {
 7         Print print = new Print();
 8         new Thread(()->{
 9             for (int i=0;i<5;i++) {
10                 print.print5();
11             }
12         },"A线程").start();
13         new Thread(()->{
14             for (int i=0;i<5;i++) {
15                 print.print10();
16             }
17         },"B线程").start();
18         new Thread(()->{
19             for (int i=0;i<5;i++) {
20                 print.print15();
21             }
22         },"C线程").start();
23     }
24 }
25 class{The Print
 26 is      Private  int COUNT =. 3 ;
 27      // use the lock latch 
28      Lock lock = new new of ReentrantLock ();
 29      for condition Condition conditio1 lock.newCondition = ();   // a key 
30      for condition Condition conditio2 lock.newCondition = ();   / / a key 
31 is      for condition Condition conditio3 lock.newCondition = ();   // a key 
32  
33 is      public  void print5 () {
 34 is          Lock.lock ();
 35          the try {
 36              // Analyzing 
37 [              the while ! (COUNT. 1 =){
38                 conditio1.await();
39             }
40             //干活
41             for (int i = 1; i <=5; i++) {
42                 System.out.print(Thread.currentThread().getName()+i);
43                 System.out.println();
44             }
45             //通知
46             count=2;
47             conditio2.signal();
48         } catch (Exception e) {
49             e.printStackTrace();
50         } the finally {
 51 is              lock.unlock ();
 52 is          }
 53 is      }
 54 is  
55      public  void print10 () {
 56 is          Lock.lock ();
 57 is          the try {
 58              // determining determines that multiple threads must be used while, after the thread wakes up again to judge, otherwise it will create a false wake-up (three threads and more will go wrong) 
59              the while (COUNT = 2! {)
 60                  conditio2.await ();
 61              }
 62              // work 
63              for ( int i = 1; i <= 10; I ++ ) {
 64                 System.out.print(Thread.currentThread().getName()+i);
65                 System.out.println();
66             }
67             //通知
68             count=3;
69             conditio3.signal();
70         } catch (Exception e) {
71             e.printStackTrace();
72         } finally {
73             lock.unlock();
74         }
75     }
76     public void print15(){
77         lock.lock();
78         try {
80             while(count!=3){
81                 conditio3.await();
82             }
84             for (int i = 1; i <=15; i++) {
85                 System.out.print(Thread.currentThread().getName()+i);
86                 System.out.println();
87             }
89             count=1;
90             conditio1.signal();
91         } catch (Exception e) {
92             e.printStackTrace();
93         } finally {
94             lock.unlock();
95         }
96     }
97 }

 

 

 

Guess you like

Origin www.cnblogs.com/fangtingfei/p/12008501.html