ccse(CountDownLatch,CycliBarrier,Semaplore,Exchanger)

Posted it馅儿包子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ccse(CountDownLatch,CycliBarrier,Semaplore,Exchanger)相关的知识,希望对你有一定的参考价值。

关于等待状态的线程调用interrupt方法报异常:InterruptedException

当线程被阻塞,比如wait,join,sleep等,在调用interrupt方法,没有占用cpu运行的线程是不可能给自己的中断状态置位的,这就产生了InterruptedException异常.

 

 

一.CountDownLatch(允许一个或多个线程等待其它线程完成操作)

代码实例:

package com.thread.ccse;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: 周强
 * Description:
 * Date: 2017/8/9
 * Time: 21:04
 */
public class CountDownLatchTest {
    public static CountDownLatch c = new CountDownLatch(4);
    public static void main(String [] args) throws InterruptedException {
        Thread t =new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(c.getCount()); //第1
                c.countDown();
                System.out.println(c.getCount());//第2
                c.countDown();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(c.getCount());//第3
                c.countDown();
                System.out.println(c.getCount());//第4
            }
        });
        t.start();
        c.await(2, TimeUnit.SECONDS);
        c.countDown();
        System.out.println(c.getCount( ));//第5
    }
}

  输出结果如下:

Connected to the target VM, address: ‘127.0.0.1:55816‘, transport: ‘socket‘
4      第1
3      第2
1      第5
Disconnected from the target VM, address: ‘127.0.0.1:55816‘, transport: ‘socket‘
1      第3
0      第4

Process finished with exit code 0

  由于线程t沉睡时间大于CountDownLatch c的等待时间,所以线程t在主线证执行完毕后才结束的.

 

二:CyclicBarrier(指定线程数,必须在该线程数内的线程全部到达同步点,才继续往下执行)

示例代码:

package com.thread.ccse;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by IntelliJ IDEA.
 * User: 周强
 * Description:
 * Date: 2017/8/9
 * Time: 21:30
 */
public class CyclicBarrierTest {

    static Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("3");
        }
    });

    static  Thread thread = null;
    public static CyclicBarrier c = new CyclicBarrier(2,t);
    static {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    c.await();
                    System.out.println("1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        });
    }




    public static void main(String [] args){

        thread.start();
        try {
            c.await();
            System.out.println("2");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

}

 执行结果:

Disconnected from the target VM, address: ‘127.0.0.1:56157‘, transport: ‘socket‘
3
2
1//其中,3 肯定是最先输出的,1 和 2 的输出顺序是不一定的 

Process finished with exit code 0

  

CountDownLatch和CyclicBarrier的区别:

CountDownLatch计数器只能使用一次,而CyclicBarrier的计数器可以使用多次,因为CyclicBarrier可以使用reset方法重置计数器;

CyclicBarrier还提供其它方法,如getNumberWating方法可以获得CyclicBarrier阻塞的线程数,isBroken方法可以用来了解阻塞的线程是否被中断。

三:

待续

 

以上是关于ccse(CountDownLatch,CycliBarrier,Semaplore,Exchanger)的主要内容,如果未能解决你的问题,请参考以下文章

java多线程-CountDownLatch

Dockerfile07 -- Dockerfile构建python

Dockerfile07 -- Dockerfile构建python

Java CountDownLatch解析(下)

CountDownLatch的使用

CountDownLatch源码分析