zoukankan      html  css  js  c++  java
  • Thread.join(), CountDownLatch、CyclicBarrier和 Semaphore区别,联系及应用

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法, 由于Thread.join()也和这三个类有类似用法,我也一起拿来进行比较。

    1. Join:

    等待当前线程执行完再接着执行主线程

    注意:一定是先Thread.start()再Thread.join(),不然join不生效。而且join最好紧跟在start后面(下面有个例子说明为什么要这样)

    2. CountDownLatch :

    A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

    允许一个线程等待其他线程完成了事情后才执行。常用于等待事件结束

    3. CyclicBarrier :

    A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.

    允许一组线程互相等待,等待至指定的屏障点,一起运行。常用于等待线程

    4. Semaphore:

    A counting semaphore. Conceptually, a semaphore maintains a set of permits.

    是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源

    一、Join

    join用于让当前执行线程等待join线程执行结束。其实现原理就是不停检查join线程是否存活,如果join线程存活则当前线程永远等待;

    public class JoinTest {
        /**
         * 场景:需要解析一个Excel中多个sheet的数据,此时可以考虑使用多线程,
         * 每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序
         * 需要提示解析完成。在这个需求中要实现主线程等待所有线程完成sheet的解析操作,
         * 以下是用join方法来处理
         * @param args
         */
        public static void main(String[] args) throws Exception{
            Thread p1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Read Excel Sheet One Data");
                }
            });
    
            Thread p2 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Read Excel Sheet Two Data");
                }
            });
            p1.start();
            p2.start();
    
            p1.join();
            p2.join();
    
            System.out.println("Excel sheet read over!!!");
    
            executProcessByOrder();
        }
    
        /**
         * 场景:现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
         */
        public static void executProcessByOrder() throws Exception{
            Thread p1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("p1 execut");
                }
            });
            Thread p2 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("p2 execut");
                }
            });
            Thread p3 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("p3 execut");
                }
            });
    
            p1.start();
            p1.join();
    
            p2.start();
            p2.join();
    
            p3.start();
            p3.join();
        }
    }
    

      运行结果如下:

    Read Excel Sheet Two Data
    Read Excel Sheet One Data
    Excel sheet read over!!!
    
    p1 execut
    p2 execut
    p3 execut
    

      为了更好理解源码如下:

       /**
         * Waits for this thread to die.
         *
         * <p> An invocation of this method behaves in exactly the same
         * way as the invocation
         *
         * <blockquote>
         * {@linkplain #join(long) join}{@code (0)}
         * </blockquote>
         *
         * @throws  InterruptedException
         *          if any thread has interrupted the current thread. The
         *          <i>interrupted status</i> of the current thread is
         *          cleared when this exception is thrown.
         */
        public final void join() throws InterruptedException {
            join(0);
        }
    
        /**
         * Waits at most {@code millis} milliseconds for this thread to
         * die. A timeout of {@code 0} means to wait forever.
         *
         * <p> This implementation uses a loop of {@code this.wait} calls
         * conditioned on {@code this.isAlive}. As a thread terminates the
         * {@code this.notifyAll} method is invoked. It is recommended that
         * applications not use {@code wait}, {@code notify}, or
         * {@code notifyAll} on {@code Thread} instances.
         *
         * @param  millis
         *         the time to wait in milliseconds
         *
         * @throws  IllegalArgumentException
         *          if the value of {@code millis} is negative
         *
         * @throws  InterruptedException
         *          if any thread has interrupted the current thread. The
         *          <i>interrupted status</i> of the current thread is
         *          cleared when this exception is thrown.
         */
        public final synchronized void join(long millis)
        throws InterruptedException {
            long base = System.currentTimeMillis();
            long now = 0;
    
            if (millis < 0) {
                throw new IllegalArgumentException("timeout value is negative");
            }
    
            if (millis == 0) {
                while (isAlive()) {
                    wait(0);
                }
            } else {
                while (isAlive()) {
                    long delay = millis - now;
                    if (delay <= 0) {
                        break;
                    }
                    wait(delay);
                    now = System.currentTimeMillis() - base;
                }
            }
        }
    

      直到Join线程终止后,线程的this.notifyAll()方法会被调用,调用notifyAll()方法是在JVM里实现的,所以在JDK里看不到,可以查看JVM源码。

    注意:一定是先Thread.start()再Thread.join(),不然join不生效。而且join最好紧跟在start后面(下面有个例子说明为什么要这样)

    public class JoinTest extends Thread {
        int i;
        public JoinTest(int i) {
            this.i = i;
        }
    
        @Override
        public void run() {
            System.out.println("num:" + i);
        }
    
        public static void main(String[] args) throws InterruptedException {
            JoinTest joinDemo1 = new JoinTest(1);
            JoinTest joinDemo2 = new JoinTest(2);
            joinDemo2.start();
    
            System.out.println("1111");
            joinDemo1.start();
            System.out.println("2222");
            joinDemo1.join();
            System.out.println("3333");
            joinDemo2.join();
            System.out.println("4444");
        }
    }

    按照join方法的定义,认为是先输出:3333再输出num 2,其实运行其中的一个结果是:

    1111
    num:2
    2222
    num:1
    3333
    4444

    这个结果与我们猜想的结果是有出入的,原因就是start与join的中间做了打印操作,并没有达到我们想要的结果,正确的写法如下:

    public class JoinTest extends Thread {
        int i;
        public JoinTest(int i) {
            this.i = i;
        }
    
        @Override
        public void run() {
            System.out.println("num:" + i);
        }
    
        public static void main(String[] args) throws InterruptedException {
            JoinTest joinDemo1 = new JoinTest(1);
            JoinTest joinDemo2 = new JoinTest(2);
            joinDemo2.start();
            joinDemo2.join();
            System.out.println("1111");
            joinDemo1.start();
            joinDemo1.join();
            System.out.println("2222");
        }
    }

    输出结果如下:

    num:2
    1111
    num:1
    2222

    一.CountDownLatch用法

    CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。

    CountDownLatch类只提供了一个构造器:

     
    public CountDownLatch(int count) {  };  //参数count为计数值
    

      

    然后下面这3个方法是CountDownLatch类中最重要的方法:

    public void await() throws InterruptedException { };   //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
    public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };  //和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行
    public void countDown() { };  //将count值减1
    

      

    下面看一个例子大家就清楚CountDownLatch的用法了:

     
    import java.util.concurrent.CountDownLatch;
    
    public class CountDownLatchTest {
    
        static CountDownLatch countDownLatch = new CountDownLatch(2);
    
        /**
         * 场景:需要解析一个Excel中多个sheet的数据,此时可以考虑使用多线程,
         * 每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序
         * 需要提示解析完成。在这个需求中要实现主线程等待所有线程完成sheet的解析操作,
         * 以下是用CountDownLatch方法来处理
         * @param args
         */
        public static void main(String[] args) throws Exception{
            Thread p1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Read Excel Sheet One Data");
                    countDownLatch.countDown();
                }
            });
    
            Thread p2 = new Thread(new Runnable() {
                public void run() {
                    System.out.println("Read Excel Sheet Two Data");
                    countDownLatch.countDown();
                }
            });
            p1.start();
            p2.start();
    
            countDownLatch.await();
    
            System.out.println("Excel sheet read over!!!");
        }
    }
    

      运行结果如下:

    Read Excel Sheet One Data
    Read Excel Sheet Two Data
    Excel sheet read over!!!
    

      CountDownLatch的构造函数接收一个int类型的参数作为计数器,如果你想等待N个点完成,这里就传入N。当调用CountDownLatch的countDown()方法时,N就会减1,CountDownLatch的await()方法会阻塞当前线程,直到N变为0。计数器必须大于等于0,只是等于0时候,计数器就是0,调用await方法时不会阻塞当前线程。

    /**
         * Decrements the count of the latch, releasing all waiting threads if
         * the count reaches zero.
         *
         * <p>If the current count is greater than zero then it is decremented.
         * If the new count is zero then all waiting threads are re-enabled for
         * thread scheduling purposes.
         *
         * <p>If the current count equals zero then nothing happens.
         */
        public void countDown() {
            sync.releaseShared(1);
        }
    
    /**
         * Releases in shared mode.  Implemented by unblocking one or more
         * threads if {@link #tryReleaseShared} returns true.
         *
         * @param arg the release argument.  This value is conveyed to
         *        {@link #tryReleaseShared} but is otherwise uninterpreted
         *        and can represent anything you like.
         * @return the value returned from {@link #tryReleaseShared}
         */
        public final boolean releaseShared(int arg) {
            if (tryReleaseShared(arg)) {
                doReleaseShared();
                return true;
            }
            return false;
        }
    
    /**
         * Causes the current thread to wait until the latch has counted down to
         * zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
         *
         * <p>If the current count is zero then this method returns immediately.
         *
         * <p>If the current count is greater than zero then the current
         * thread becomes disabled for thread scheduling purposes and lies
         * dormant until one of two things happen:
         * <ul>
         * <li>The count reaches zero due to invocations of the
         * {@link #countDown} method; or
         * <li>Some other thread {@linkplain Thread#interrupt interrupts}
         * the current thread.
         * </ul>
         *
         * <p>If the current thread:
         * <ul>
         * <li>has its interrupted status set on entry to this method; or
         * <li>is {@linkplain Thread#interrupt interrupted} while waiting,
         * </ul>
         * then {@link InterruptedException} is thrown and the current thread's
         * interrupted status is cleared.
         *
         * @throws InterruptedException if the current thread is interrupted
         *         while waiting
         */
        public void await() throws InterruptedException {
            sync.acquireSharedInterruptibly(1);
        }
    

      总结:调用join方法需要等待thread执行完毕才能继续向下执行,而CountDownLatch只需要检查计数器的值为零就可以继续向下执行,相比之下,CountDownLatch更加灵活一些,可以实现一些更加复杂的业务场景。

     

    二.CyclicBarrier用法

    字面意思回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行。叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用。我们暂且把这个状态就叫做barrier,当调用await()方法之后,线程就处于barrier了。

    CyclicBarrier类位于java.util.concurrent包下,CyclicBarrier提供2个构造器:

    public CyclicBarrier(int parties, Runnable barrierAction) {
    }
     
    public CyclicBarrier(int parties) {
    }

    参数parties指让多少个线程或者任务等待至barrier状态;参数barrierAction为当这些线程都达到barrier状态时会执行的内容。

    然后CyclicBarrier中最重要的方法就是await方法,它有2个重载版本:

     
    public int await() throws InterruptedException, BrokenBarrierException { };
    public int await(long timeout, TimeUnit unit)throws InterruptedException,BrokenBarrierException,TimeoutException { };
    

      

    第一个版本比较常用,用来挂起当前线程,直至所有线程都到达barrier状态再同时执行后续任务;

    第二个版本是让这些线程等待至一定的时间,如果还有线程没有到达barrier状态就直接让到达barrier的线程执行后续任务。

    下面举几个例子就明白了:

    假若有若干个线程都要进行写数据操作,并且只有所有线程都完成写数据操作之后,这些线程才能继续做后面的事情,此时就可以利用CyclicBarrier了:

     
    public class Test {
        public static void main(String[] args) {
            int N = 4;
            CyclicBarrier barrier  = new CyclicBarrier(N);
            for(int i=0;i<N;i++)
                new Writer(barrier).start();
        }
        static class Writer extends Thread{
            private CyclicBarrier cyclicBarrier;
            public Writer(CyclicBarrier cyclicBarrier) {
                this.cyclicBarrier = cyclicBarrier;
            }
     
            @Override
            public void run() {
                System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据...");
                try {
                    Thread.sleep(5000);      //以睡眠来模拟写入数据操作
                    System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕,等待其他线程写入完毕");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch(BrokenBarrierException e){
                    e.printStackTrace();
                }
                System.out.println("所有线程写入完毕,继续处理其他任务...");
            }
        }
    }
    

      

    执行结果:

    线程Thread-0正在写入数据...
    线程Thread-3正在写入数据...
    线程Thread-2正在写入数据...
    线程Thread-1正在写入数据...
    线程Thread-2写入数据完毕,等待其他线程写入完毕
    线程Thread-0写入数据完毕,等待其他线程写入完毕
    线程Thread-3写入数据完毕,等待其他线程写入完毕
    线程Thread-1写入数据完毕,等待其他线程写入完毕
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...
    

      

    从上面输出结果可以看出,每个写入线程执行完写数据操作之后,就在等待其他线程写入操作完毕。

    当所有线程线程写入操作完毕之后,所有线程就继续进行后续的操作了。

    如果说想在所有线程写入操作完之后,进行额外的其他操作可以为CyclicBarrier提供Runnable参数

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    public class Test {
        public static void main(String[] args) {
            int N = 4;
            CyclicBarrier barrier  = new CyclicBarrier(N,new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前线程"+Thread.currentThread().getName());  
                }
            });
     
            for(int i=0;i<N;i++)
                new Writer(barrier).start();
        }
        static class Writer extends Thread{
            private CyclicBarrier cyclicBarrier;
            public Writer(CyclicBarrier cyclicBarrier) {
                this.cyclicBarrier = cyclicBarrier;
            }
     
            @Override
            public void run() {
                System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据...");
                try {
                    Thread.sleep(5000);      //以睡眠来模拟写入数据操作
                    System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕,等待其他线程写入完毕");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch(BrokenBarrierException e){
                    e.printStackTrace();
                }
                System.out.println("所有线程写入完毕,继续处理其他任务...");
            }
        }
    }

    运行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    线程Thread-0正在写入数据...
    线程Thread-1正在写入数据...
    线程Thread-2正在写入数据...
    线程Thread-3正在写入数据...
    线程Thread-0写入数据完毕,等待其他线程写入完毕
    线程Thread-1写入数据完毕,等待其他线程写入完毕
    线程Thread-2写入数据完毕,等待其他线程写入完毕
    线程Thread-3写入数据完毕,等待其他线程写入完毕
    当前线程Thread-3
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...
    所有线程写入完毕,继续处理其他任务...

    从结果可以看出,当四个线程都到达barrier状态后,会从四个线程中选择一个线程去执行Runnable。

    下面看一下为await指定时间的效果:

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    public class Test {
        public static void main(String[] args) {
            int N = 4;
            CyclicBarrier barrier  = new CyclicBarrier(N);
     
            for(int i=0;i<N;i++) {
                if(i<N-1)
                    new Writer(barrier).start();
                else {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    new Writer(barrier).start();
                }
            }
        }
        static class Writer extends Thread{
            private CyclicBarrier cyclicBarrier;
            public Writer(CyclicBarrier cyclicBarrier) {
                this.cyclicBarrier = cyclicBarrier;
            }
     
            @Override
            public void run() {
                System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据...");
                try {
                    Thread.sleep(5000);      //以睡眠来模拟写入数据操作
                    System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕,等待其他线程写入完毕");
                    try {
                        cyclicBarrier.await(2000, TimeUnit.MILLISECONDS);
                    } catch (TimeoutException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch(BrokenBarrierException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"所有线程写入完毕,继续处理其他任务...");
            }
        }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    线程Thread-0正在写入数据...
    线程Thread-2正在写入数据...
    线程Thread-1正在写入数据...
    线程Thread-2写入数据完毕,等待其他线程写入完毕
    线程Thread-0写入数据完毕,等待其他线程写入完毕
    线程Thread-1写入数据完毕,等待其他线程写入完毕
    线程Thread-3正在写入数据...
    java.util.concurrent.TimeoutException
    Thread-1所有线程写入完毕,继续处理其他任务...
    Thread-0所有线程写入完毕,继续处理其他任务...
        at java.util.concurrent.CyclicBarrier.dowait(Unknown Source)
        at java.util.concurrent.CyclicBarrier.await(Unknown Source)
        at com.cxh.test1.Test$Writer.run(Test.java:58)
    java.util.concurrent.BrokenBarrierException
        at java.util.concurrent.CyclicBarrier.dowait(Unknown Source)
        at java.util.concurrent.CyclicBarrier.await(Unknown Source)
        at com.cxh.test1.Test$Writer.run(Test.java:58)
    java.util.concurrent.BrokenBarrierException
        at java.util.concurrent.CyclicBarrier.dowait(Unknown Source)
        at java.util.concurrent.CyclicBarrier.await(Unknown Source)
        at com.cxh.test1.Test$Writer.run(Test.java:58)
    Thread-2所有线程写入完毕,继续处理其他任务...
    java.util.concurrent.BrokenBarrierException
    线程Thread-3写入数据完毕,等待其他线程写入完毕
        at java.util.concurrent.CyclicBarrier.dowait(Unknown Source)
        at java.util.concurrent.CyclicBarrier.await(Unknown Source)
        at com.cxh.test1.Test$Writer.run(Test.java:58)
    Thread-3所有线程写入完毕,继续处理其他任务...

    上面的代码在main方法的for循环中,故意让最后一个线程启动延迟,因为在前面三个线程都达到barrier之后,等待了指定的时间发现第四个线程还没有达到barrier,就抛出异常并继续执行后面的任务。

    另外CyclicBarrier是可以重用的,看下面这个例子:

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    public class Test {
        public static void main(String[] args) {
            int N = 4;
            CyclicBarrier barrier  = new CyclicBarrier(N);
     
            for(int i=0;i<N;i++) {
                new Writer(barrier).start();
            }
     
            try {
                Thread.sleep(25000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
     
            System.out.println("CyclicBarrier重用");
     
            for(int i=0;i<N;i++) {
                new Writer(barrier).start();
            }
        }
        static class Writer extends Thread{
            private CyclicBarrier cyclicBarrier;
            public Writer(CyclicBarrier cyclicBarrier) {
                this.cyclicBarrier = cyclicBarrier;
            }
     
            @Override
            public void run() {
                System.out.println("线程"+Thread.currentThread().getName()+"正在写入数据...");
                try {
                    Thread.sleep(5000);      //以睡眠来模拟写入数据操作
                    System.out.println("线程"+Thread.currentThread().getName()+"写入数据完毕,等待其他线程写入完毕");
     
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch(BrokenBarrierException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"所有线程写入完毕,继续处理其他任务...");
            }
        }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    线程Thread-0正在写入数据...
    线程Thread-1正在写入数据...
    线程Thread-3正在写入数据...
    线程Thread-2正在写入数据...
    线程Thread-1写入数据完毕,等待其他线程写入完毕
    线程Thread-3写入数据完毕,等待其他线程写入完毕
    线程Thread-2写入数据完毕,等待其他线程写入完毕
    线程Thread-0写入数据完毕,等待其他线程写入完毕
    Thread-0所有线程写入完毕,继续处理其他任务...
    Thread-3所有线程写入完毕,继续处理其他任务...
    Thread-1所有线程写入完毕,继续处理其他任务...
    Thread-2所有线程写入完毕,继续处理其他任务...
    CyclicBarrier重用
    线程Thread-4正在写入数据...
    线程Thread-5正在写入数据...
    线程Thread-6正在写入数据...
    线程Thread-7正在写入数据...
    线程Thread-7写入数据完毕,等待其他线程写入完毕
    线程Thread-5写入数据完毕,等待其他线程写入完毕
    线程Thread-6写入数据完毕,等待其他线程写入完毕
    线程Thread-4写入数据完毕,等待其他线程写入完毕
    Thread-4所有线程写入完毕,继续处理其他任务...
    Thread-5所有线程写入完毕,继续处理其他任务...
    Thread-6所有线程写入完毕,继续处理其他任务...
    Thread-7所有线程写入完毕,继续处理其他任务...

    从执行结果可以看出,在初次的4个线程越过barrier状态后,又可以用来进行新一轮的使用。而CountDownLatch无法进行重复使用。

    三.Semaphore用法

    Semaphore翻译成字面意思为 信号量,Semaphore可以控同时访问的线程个数,通过 acquire() 获取一个许可,如果没有就等待,而 release() 释放一个许可。

    Semaphore类位于java.util.concurrent包下,它提供了2个构造器:

     
    1
    2
    3
    4
    5
    6
    public Semaphore(int permits) {          //参数permits表示许可数目,即同时可以允许多少线程进行访问
        sync = new NonfairSync(permits);
    }
    public Semaphore(int permits, boolean fair) {    //这个多了一个参数fair表示是否是公平的,即等待时间越久的越先获取许可
        sync = (fair)? new FairSync(permits) : new NonfairSync(permits);
    }

    下面说一下Semaphore类中比较重要的几个方法,首先是acquire()、release()方法:

     
    1
    2
    3
    4
    public void acquire() throws InterruptedException {  }     //获取一个许可
    public void acquire(int permits) throws InterruptedException { }    //获取permits个许可
    public void release() { }          //释放一个许可
    public void release(int permits) { }    //释放permits个许可

    acquire()用来获取一个许可,若无许可能够获得,则会一直等待,直到获得许可。

    release()用来释放许可。注意,在释放许可之前,必须先获获得许可。

    这4个方法都会被阻塞,如果想立即得到执行结果,可以使用下面几个方法:

    1
    2
    3
    4
    public boolean tryAcquire() { };    //尝试获取一个许可,若获取成功,则立即返回true,若获取失败,则立即返回false
    public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException { };  //尝试获取一个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false
    public boolean tryAcquire(int permits) { }; //尝试获取permits个许可,若获取成功,则立即返回true,若获取失败,则立即返回false
    public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException { }; //尝试获取permits个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false

    另外还可以通过availablePermits()方法得到可用的许可数目。

    下面通过一个例子来看一下Semaphore的具体使用:

    假若一个工厂有5台机器,但是有8个工人,一台机器同时只能被一个工人使用,只有使用完了,其他工人才能继续使用。那么我们就可以通过Semaphore来实现:

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    public class Test {
        public static void main(String[] args) {
            int N = 8;            //工人数
            Semaphore semaphore = new Semaphore(5); //机器数目
            for(int i=0;i<N;i++)
                new Worker(i,semaphore).start();
        }
     
        static class Worker extends Thread{
            private int num;
            private Semaphore semaphore;
            public Worker(int num,Semaphore semaphore){
                this.num = num;
                this.semaphore = semaphore;
            }
     
            @Override
            public void run() {
                try {
                    semaphore.acquire();
                    System.out.println("工人"+this.num+"占用一个机器在生产...");
                    Thread.sleep(2000);
                    System.out.println("工人"+this.num+"释放出机器");
                    semaphore.release();          
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    工人0占用一个机器在生产...
    工人1占用一个机器在生产...
    工人2占用一个机器在生产...
    工人4占用一个机器在生产...
    工人5占用一个机器在生产...
    工人0释放出机器
    工人2释放出机器
    工人3占用一个机器在生产...
    工人7占用一个机器在生产...
    工人4释放出机器
    工人5释放出机器
    工人1释放出机器
    工人6占用一个机器在生产...
    工人3释放出机器
    工人7释放出机器
    工人6释放出机器

    下面对上面说的三个辅助类进行一个总结:

    1)CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同:

    CountDownLatch一般用于某个线程A等待若干个其他线程执行完任务之后,它才执行;

    而CyclicBarrier一般用于一组线程互相等待至某个状态,然后这一组线程再同时执行;

    另外,CountDownLatch是不能够重用的,而CyclicBarrier是可以重用的。

    2)Semaphore其实和锁有点类似,它一般用于控制对某组资源的访问权限。

    参考资料:

    http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html

    http://leaver.me/archives/3220.html

    http://developer.51cto.com/art/201403/432095.htm

    http://blog.csdn.net/yanhandle/article/details/9016329

    http://blog.csdn.net/cutesource/article/details/5780740

    http://www.cnblogs.com/whgw/archive/2011/09/29/2195555.html

  • 相关阅读:
    菜鸟版JAVA设计模式—从买房子看代理模式
    NTP工作机制及时间同步的方法
    Java工厂模式
    圣魔大战3(Castle Fantisia)艾伦希亚战记完美攻略
    对javabean的内省操作
    插入排序(insertion sort)
    中英文对照 —— 十二星座
    中英文对照 —— 十二星座
    数学归纳法的相关证明
    数学归纳法的相关证明
  • 原文地址:https://www.cnblogs.com/barrywxx/p/8431942.html
Copyright © 2011-2022 走看看