Contract and synchronization components

1 Overview

java and contract already explained volatile, synchronized, CAS, AQS mechanism. These are the basis of the whole concurrency, and this mainly to talk about some of the components under the contract.

2,CountDownLatch

2.1 demo:

First demonstration of this feature components. The main thread of execution to the await () method after, synchronization will wait two threads of execution, where 2 is the constructor passed. And then execute it.

public class CountDownLatchDemo {
    public static void main(String[] args) throws Exception{
        final CountDownLatch latch = new CountDownLatch(2);

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("线程1执行...");
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace (); 
                } 

            } 
        } .start (); 

        new new the Thread () { 
            @Override 
            public  void RUN () {
                 the try { 
                    the Thread.sleep ( 1000 ); 
                    System.out.println ( "thread 2 performs ..." ); 
                    latch.countDown (); 
                } the catch (Exception E) { 
                    e.printStackTrace (); 
                } 

            } 
        } .start (); 
        System.out.println ( "... the main thread is about to execute the await" ); 
        latch.await ();
        System.out.println ( "main thread performs ..." ); 
    } 
}

2.2 constructor:

    public a CountDownLatch ( int COUNT) {
         IF (COUNT <0) the throw  new new an IllegalArgumentException ( "COUNT <0" );
         the this .sync = new new Sync (COUNT); 
    } 
        // incoming parameters as the size of the state. Here state, i.e. the state at the AQS 
        Sync ( int COUNT) { 
            the setState (COUNT); 
        }

2.3 await():

    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) < 0)
            doAcquireSharedInterruptibly(arg);
    }

We should look tryAcquireShared () method:

        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }

Obviously, state that we pass 2, not equal to 0, will enter the method doAcquireSharedInterruptibly ():

    private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

In front of this method are analyzed, the direct drawing:

Then another thread will execute countDown () method, the source logic is very simple, that is in turn subtracted from the value of the state. Then determines whether the state is equal to 0, if the state == 0, then the main thread queue wakeup. It can be found after the AQS familiar principle, these are very simple, so AQS is the basis for concurrent.

 

Guess you like

Origin www.cnblogs.com/xtz2018/p/11493982.html