Java thread communication learning (multi-threaded (Lock)) - Producer Consumer

After the upgrade version of SDK1.5 synchronized, the idea is: The synchronous lock and encapsulated into an object, which object contains actions operable lock.

Code:

  1  // 1, introducing the package 
  2  Import in java.util.concurrent.locks *;.
   . 3  class the Resource
   . 4  {
   . 5      Private String name;
   . 6      Private  int COUNT = 1 ;
   . 7      Private  Boolean In Flag = to false ;
   . 8      
  . 9      // create a mutex lock (corresponding to the synchronized) 
10      lock lock of ReentrantLock new new = ();
 . 11      // create a lock object (Object corresponding to the object) 
12 is      for condition Condition producer_con lock.newCondition = ();
 13 is      for condition Condition consumer_con lock.newCondition = ();
 14      
15     public void set(String name) 
 16     {    
 17         lock.lock();
 18         try
 19         {
 20             while(flag)
 21                 try{producer_con.await();}catch(InterruptedException e){}
 22             this.name=name+count;
 23             count++;
 24             System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
 25             flag = true;
 26             consumer_con.signal();
 27         }
 28         finally
 29         {
 30             lock.unlock();
 31         }
 32     }
 33     
 34     public void out()
 35     {
 36         lock.lock();
 37         try
 38         {
 39             while(!flag)
 40                 try{consumer_con.await();}catch(InterruptedException e){}
 41             System.out.println (Thread.currentThread () getName () + "== consumer ==" +. The this .name);
 42 is              In Flag = to false ;
 43 is              producer_con.signal ();
 44 is          }
 45          the finally 
46 is          {
 47              Lock .unlock ();
 48          }
 49      }
 50  }
 51 is  
52 is  // custom thread tasks (producer thread) 
53 is  class producer the implements the Runnable
 54 is  {
 55      Private the Resource R & lt;
 56 is      
57 is     Producer (R & lt the Resource)
 58      {
 59          the this .r = R & lt;
 60      }
 61 is      public  void RUN ()
 62 is      {
 63 is          the while ( to true )
 64          {
 65              r.set ( "product" );
 66          }
 67      }
 68  }
 69  // defined task thread (thread consumer) 
70  class consumer the implements the Runnable
 71 is  {
 72      Private the Resource R & lt;
 73 is      
74     Consumer(Resource r)
 75     {
 76         this.r=r;
 77     }
 78     
 79     public void run()
 80     {
 81         while(true)
 82         {
 83             r.out();
 84         }
 85     }
 86 }
 87 
 88 class ProducerConsumerDemo
 89 {
 90     public static void main(String[] args)
 91     {
 92         //Examples of shared resources 
93          the Resource = R & lt new new the Resource ();
 94          // instantiate threaded task, specify the shared resources 
95          Producer P = new new Producer (R & lt);
 96          Consumer C = new new Consumer (R & lt);
 97          // instantiate thread specifying the task thread 
98          the thread = T0 new new the thread (P);
 99          the thread = T1 new new the thread (P);
 100          the thread T2 = new new the thread (C);
 101          the thread T3 = new new the thread (C);
 102          // open thread, the method of execution threads run tasks 
103         t0.start();
104         t1.start();
105         t2.start();
106         t3.start();
107     }
108 }

 

result:

 

lock synchronization is to package it has a relationship and differences:

 

Guess you like

Origin www.cnblogs.com/WarBlog/p/12083402.html