Concurrent with high concurrency - concurrent simulation code

A, CountDownLatch

1. Background:

  (1) countDownLatch is introduced java1.5, saying tools also CyclicBarrier, Semaphore, concurrentHashMap BlockingQueue together and introduced.

  (2) the presence of at java.util.cucurrent package.

2. concept

  (1) countDownLatch this class to make a thread wait for another thread is finished before their execution.

  (2) is achieved by means of a counter, the initial value of the counter is the number of threads. Whenever a thread is finished, the value of the counter is -1 when the counter is 0, indicating that all threads are executed, and then to the latch waiting thread can return to work.

3. Source

  (1) countDownLatch only provides a class constructor:

// parameter count is a count value 
public a CountDownLatch ( int count) {};  

  (2) class has three methods are the most important:

// call await () method of the thread is suspended, it will wait until the count is 0 before continuing execution 
public  void await () throws InterruptedException {};   
 // and await () is similar, but the wait for a certain time after count value becomes 0, then it will not proceed to 
public  Boolean the await ( Long timeout, TimeUnit Unit) throws InterruptedException {};  
 // the count value is decreased. 1 
public  void the countDown () {};  

 

Example 4

Common examples:

public class CountDownLatchTest {

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(2);
        System.out.println ( "main thread begins execution ...... ......" );
         // first child thread execution 
        ExecutorService ES1 = Executors.newSingleThreadExecutor ();
        es1.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println ( . "Child thread:" + Thread.currentThread () getName ( ) + " execution" );
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                latch.countDown();
            }
        });
        es1.shutdown();

        // The second sub-thread execution 
        ExecutorService ES2 = Executors.newSingleThreadExecutor ();
        es2.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
                System.out.println ( . "Child thread:" + Thread.currentThread () getName ( ) + " execution" );
                latch.countDown();
            }
        });
        es2.shutdown();
        System.out.println ( "wait two threads is finished ...... ......" );
         the try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        System.out.println ( "two sub-threads are finished, proceed to the main thread" );
    }
}

 

The results set:

The main thread begins execution ...... ......
Wait two threads is finished ...... ......
Child thread: the pool -1-the Thread- . 1 performed
Child thread: the pool -2-the Thread- . 1 performed
Two sub-threads are finished, continue with the main thread

 

Simulate concurrent Example:

public class Parallellimit {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        CountDownLatch cdl = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            CountRunnable runnable = new CountRunnable(cdl);
            pool.execute(runnable);
        }
    }
}

 class CountRunnable implements Runnable {
    private CountDownLatch countDownLatch;
    public CountRunnable(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    @Override
    public  void RUN () {
         the try {
             the synchronized (CountDownLatch) {
                 / ** * Every time a reduced capacity * /
                countDownLatch.countDown();
                System.out.println("thread counts = " + (countDownLatch.getCount()));
            }
            countDownLatch.await();
            System.out.println("concurrency counts = " + (100 - countDownLatch.getCount()));
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
    }
}

* CountDownLatch and CyclicBarrier difference:
1.countDownLatch is a counter, a thread completes a record, down counter, can only be used once
2.CyclicBarrier the counter like a valve, the need to reach all threads, and then continue, the counter is incremented, provide reset function can be used multiple times




Reference link: https: //www.jianshu.com/p/e233bb37d2e6

Guess you like

Origin www.cnblogs.com/jmy520/p/11874872.html