Threads in Java - set concurrency library

  Knowledge of basic threads have finished school and see a collection of Java5 concurrent provided in the library. . .

A, congestion queues may be

And not fixed queue length of queues comprises fixed length

ArrayBlockQueue only put () method and take () method that has the blocking feature

1, functions and effects of blocking queue code is as follows:

. 1  Import java.util.concurrent.ArrayBlockingQueue;
 2  Import java.util.concurrent.BlockingQueue;
 . 3  
. 4  / ** 
. 5  * @className: BlockingQueueTest
 . 6  * @description: blocking queue can be applied
 . 7  * @author : SSC
 . 8  * @ date: 2019 years. 6 morning of May 22 11:07:22
 . 9   * / 
10  public  class BlockingQueueTest {
 . 11  
12 is      public  static  void main (String [] args) {
 13 is          BlockingQueue Queue = new new ArrayBlockingQueue with (. 3 );
 14          for( Int I = 0; I <2; I ++ ) {
 15              new new the Thread () {
 16                  @Override
 . 17                  public  void RUN () {
 18 is                      the while ( to true ) {
 . 19                          the try {
 20 is                              the Thread.sleep (( Long ) (Math.random () * 10000 ));
 21 is                              System.out.println (Thread.currentThread () getName () + "data ready release." );
 22                              // to put data on the column 
23 is                              queue.put (. 1 );
 24                             System.out.println (Thread.currentThread () getName () + " has been put data queue currently has" + queue.size () + "data." );
 25                          } the catch (Exception E) {
 26 is                              E. printStackTrace ();
 27                          }
 28                      }
 29                  }
 30              } .start ();
 31 is          }
 32          
33 is          new new the Thread () {
 34 is              @Override
 35              public  void RUN () {
 36                  the while ( to true ) {
 37 [                     the try {
 38 is                          the Thread.sleep (10000 );
 39                          System.out.println (Thread.currentThread () getName () + "ready to take data." );
 40                          // extract data from the queue 
41 is                          queue.take ();
 42 is                          System.out.println (Thread.currentThread () getName () + " data has been removed, there are queues" + queue.size () + "data." );
 43 is                      } the catch (Exception E) {
 44 is                          E. printStackTrace ();
 45                      }
 46 is                  }
 47              }
 48         }.start();
49         
50     }
51 
52 }

2, the congestion queue to implement the functions of the notification

 Code examples are as follows:

. 1  Import java.util.concurrent.ArrayBlockingQueue;
 2  Import java.util.concurrent.BlockingQueue;
 . 3  
. 4  public  class the Business {
 . 5  
. 6      Private BlockingQueue <Integer> = Queue1 new new ArrayBlockingQueue with <Integer> (. 1 );
 . 7      Private BlockingQueue <Integer> = Queue2 new new ArrayBlockingQueue with <Integer> (. 1 );
 . 8      
. 9      // this formulation it is anonymous constructor constructor priority is executed first before the highest, all constructors 
10      {
 . 11          the try {
 12 is              queue2.put (. 1 );
 13         } The catch (InterruptedException E) {
 14              e.printStackTrace ();
 15          }
 16      }
 . 17      public  void Sub ( int I) {
 18 is  
. 19          the try {
 20 is              // queue to put a value 
21 is              queue1.put (1 );
 22 is              for ( int J =. 1; J <= 10; J ++ ) {
 23 is                  System.out.println ( "Sub Thread Sequece of" + J + ", Loop of" + I);
 24              }
 25              // queue value should take 2 out 
26             queue2.take();
27             
28         } catch (InterruptedException e) {
29             e.printStackTrace();
30         }
31     }
32 
33     public void main(int i) {
34         try {
35             queue2.put(1);
36             for (int j = 1; j <= 100; j++) {
37                 System.out.println("main thread sequece of " + j + ", loop of " + i);
38             }
39             queue1.take();
40         } catch (InterruptedException e) {
41             e.printStackTrace();
42         }
43         
44     }
45 }

Second, synchronous set (concurrent collection) class

Traditional collection when concurrent access is problematic

Java5 provides some synchronized collections:

ConcurrentMap

CopyOnWriteArrayList

CopyOnWriteArraySet

It is that these collections are thread-safe, even in a multithreaded environment, concurrency problems do not exist, and basic usage is a collection of classes are the same, but the JDK achieve a thread synchronization code! ! !

 

Guess you like

Origin www.cnblogs.com/ssh-html/p/11070683.html