线程协作类总结
Posted 左沩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程协作类总结相关的知识,希望对你有一定的参考价值。
1、 CountDownLatch允许一个或多个线程等待其他线程完成操作。当等待操作完成时,主线程继续往下执行
/**
* 当 countDownLatch.await(); 方法触发时,必须等待执行指定次数的countDownLatch.countDown();方法后,才能继续往下执行
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException
final CountDownLatch countDownLatch = new CountDownLatch(10);
for (int i = 0; i < 10; i++)
final int number = i + 1;
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
System.out.println("执行任务[" + number + "]");
countDownLatch.countDown();
System.out.println("完成任务[" + number + "]");
;
Thread thread = new Thread(runnable);
thread.start();
System.out.println("主线程开始等待...");
//当执行这个方法后,需要等待 countDownLatch.countDown();执行10次才能放弃等待
countDownLatch.await();
System.out.println("主线程执行完毕...");
2、CyclicBarrier 方法,保证所有线程在同一起跑线上开始运行。当执行CyclicBarrier 的await方法时,这个线程被中断了,只有指定个数的await方法执行完成时,当前线程才能继续网下执行,也可以用来完成CountDownLatch的操作。
/**
* cyclicBarrier 当cyclicBarrier.await();这个被触发时,cyclicBarrier.await();必须被执行10次后才会被唤醒
* @param args
*/
public static void main(String[] args)
final CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
for (int i = 0; i < 10; i++)
final int number = i + 1;
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
System.out.println("等待执行任务[" + number + "]");
try
cyclicBarrier.await();
catch (InterruptedException e)
catch (BrokenBarrierException e)
System.out.println("开始执行任务[" + number + "]");
;
Thread thread = new Thread(runnable);
thread.start();
3、Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源
/**
* semaphore.acquire();执行时,获取一个许可证,当获取的许可证超过设定数目时,线程进入阻塞等待阶段,通过semaphore.release();
* 可以释放许可证
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException
final Semaphore semaphore = new Semaphore(10);
final AtomicInteger number = new AtomicInteger();
for (int i = 0; i < 100; i++)
Runnable runnable = new Runnable()
public void run()
try
Thread.sleep(1000);
catch (InterruptedException e)
try
semaphore.acquire();
synchronized (this)
number.incrementAndGet();
System.out.println(number.get());
semaphore.release();
catch (InterruptedException e)
;
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(1000);
System.out.println("共" + number.get() + "个线程获得到信号");
System.exit(0);
3、Exchanger(交换者)是一个用于线程间协作的工具类。Exchanger用于进行线程间的数据交换。它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据。这两个线程通过exchange方法交换数据,如果第一个线程先执行exchange()方法,它会一直等待第二个线程也执行exchange方法,当两个线程都到达同步点时,这两个线程就可以交换数据,将本线程生产出来的数据传递给对方。
/**
* 当现场执行到 String content = exchanger.exchange("thread1"); 等待另外一个执行此方法,交换数据
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException
final Exchanger<String> exchanger = new Exchanger<String>();
Thread thread1 = new Thread(new Runnable()
public void run()
try
System.out.println("线程1等待接受");
String content = exchanger.exchange("thread1");
System.out.println("线程1收到的为:" + content);
catch (InterruptedException e)
);
Thread thread2 = new Thread(new Runnable()
public void run()
try
System.out.println("线程2等待接受并沉睡3秒");
// Thread.sleep(3000);
String content = exchanger.exchange("thread2");
System.out.println("线程2收到的为:" + content);
catch (InterruptedException e)
);
thread1.start();
thread2.start();
以上是关于线程协作类总结的主要内容,如果未能解决你的问题,请参考以下文章