CountDownLatch,CyclicBarrier,Semaphore

来源:互联网 发布:天视6.3监控软件 编辑:程序博客网 时间:2024/06/07 09:54

首先 说下 3者个人理解的区别:

CountDownLatch:个人感觉他的功能是 有一个线程 中 开启了多个线程,然后这个线程要等待开启的所有线程结束后才能继续执行。

CyclicBarrier:个人感觉就是多个线程运行到一个点然后等待多个线程都运行到那个点的时候 所有线程继续一起执行 类似于赛跑

Semaphore:就是用来控制一个资源可以同时被几个人访问的 有点类似于锁


CountDownLatch列子:

package thred;import java.util.concurrent.CountDownLatch;public class MyCountDownLatchThread implements Runnable{private CountDownLatch latch;private String i;public MyCountDownLatchThread(CountDownLatch latch, String i) {super();this.latch = latch;this.i = i;}@Overridepublic void run() {System.out.println("第"+i+"个线程准备run");try {Thread.sleep(10000);} catch (Exception e) {}System.out.println("第"+i+"个线程结束run");latch.countDown();}}

package thred;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * CountDownLatch的使用 * @author djk * */public class MyCountDownLatchTest{public static void main(String[] args) throws InterruptedException {ExecutorService saveExec = Executors.newFixedThreadPool(5);CountDownLatch latch = new CountDownLatch(5);for (int i =0;i<5;i++){saveExec.execute(new MyCountDownLatchThread(latch, i+""));}System.out.println("开始等待");latch.await();System.out.println("都结束了啊");}}

CyclicBarrier列子:

package thred;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;public class MyCyclicBarrierThrad implements Runnable{private int i;private CyclicBarrier cyclicBarrier;public MyCyclicBarrierThrad(int i, CyclicBarrier cyclicBarrier) {super();this.i = i;this.cyclicBarrier = cyclicBarrier;}@Overridepublic void run() {System.out.println("第"+i+"个任务准备好了。。。。");try {Thread.sleep(3000);} catch (Exception e) {// TODO: handle exception}try {cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e){// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("都准备好了啊 冲啊");}}

package thred;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MyCyclicBarrierTest {public static void main(String[] args) {ExecutorService saveExec = Executors.newFixedThreadPool(5);CyclicBarrier cyclicBarrier = new CyclicBarrier(5);for (int i =0;i<5;i++){saveExec.execute(new MyCyclicBarrierThrad(i, cyclicBarrier));}System.out.println("呵呵 哥先走");}}

Semaphore列子:

package thred;import java.util.concurrent.Semaphore;public class MySemaphoreThread implements Runnable{private String i;private Semaphore semaphore;public MySemaphoreThread(String i, Semaphore semaphore) {super();this.i = i;this.semaphore = semaphore;}@Overridepublic void run() {try{semaphore.acquire();System.out.println("工人:"+i+"获得锁 。。。。开始工作");Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("工人:"+i+"准备释放锁");semaphore.release();}}

package thred;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;public class MySemaphoreTest {public static void main(String[] args) {ExecutorService saveExec = Executors.newCachedThreadPool();Semaphore semaphore = new Semaphore(3);for (int i =0;i<8;i++){saveExec.execute(new MySemaphoreThread(i+"", semaphore));}}}


0 0
原创粉丝点击