java中常用的并发工具类
2017-06-27 17:13
295 查看
· 1. 等待多线程完成的CountDownLatch
构造函数接收一个int类型的参数作为计数器,如果想等待N个点,就传入N。当调用CountDownLatch的countDown方法时,N就会减一,直至减为零。使用await方法等待,当N的值变为零,执行await的线程继续执行。public class CountDownTest { static CountDownLatch c = new CountDownLatch(2); static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String agrs[]){ pool.execute(new Runnable() { public void run() { try { TimeUnit.MILLISECONDS.sleep(3); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("This is A"); c.countDown(); } }); pool.execute(new Runnable() { public void run() { try { TimeUnit.MILLISECONDS.sleep(3); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("This is B"); c.countDown(); } }); try { c.await(); }catch (InterruptedException e){ } System.out.println("This is main"); } }
2. 同步屏障CyclicBarrier
可循环使用的屏障。它要做的事情,让一组线程到达屏障时被阻塞,直到最后一个线程到达屏障时,屏障才会打开,所有被屏障拦截的线程才会继续运行。默认构造方法CyclicBarrier(int parties),其参数表示屏障拦截的线程数,每个线程调用await方法告诉CyclicBarrier已经到达屏障,然后当前线程阻塞。
public class CylicBarrierTest { static CyclicBarrier c = new CyclicBarrier(2); static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String args[]){ pool.execute(new Runnable() { public void run() { System.out.println("this is A"); try { c.await(); System.out.println("this is Aa"); }catch (Exception e){ e.printStackTrace(); } } }); pool.execute(new Runnable() { public void run() { System.out.println("this is B"); try { c.await(); System.out.println("this is Bb"); }catch (Exception e){ e.printStackTrace(); } } }); pool.shutdown(); } }
3. CountDownLatch vs CyclicBarrier
CountDownLatch的计数器只使用一次,而CyclicBarrier的计数器可以使用reset方法重置,重复使用4. 控制线程并发数的信号量 Semaphore
控制有限哥线程使用资源。使用方法 Semaphore ss = new Semaphore(N), N表示最大的并发量,每个线程使用资源前调用ss.acquire()方法获取权限,使用完后调用ss.release()方法释放权限。5. 线程间的数据交换Exchanger
它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据。public class ExchangeTest { private static final Exchanger<String> exgr = new Exchanger<String>(); private static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String args[]){ pool.execute(new Runnable() { public void run() { String A = "银行流水A"; try { A = exgr.exchange(A); System.out.println("A 当前的值:" + A); }catch (InterruptedException e){ } } }); pool.execute(new Runnable() { public void run() { String B = "银行流水B"; try { B = exgr.exchange(B); System.out.println("B 当前的值:" + B); }catch (InterruptedException e){ } } }); pool.shutdown(); }
相关文章推荐
- Java并发中常用同步工具类
- java中并发常用工具类
- java并发的艺术-读书笔记-第八章常用的并发工具类
- 常用java工具类
- 【工具类】Java常用数据类型的转换
- java开发中常用的数字工具类
- [JAVA][StringUtils]字符串工具类的常用方法
- 常用的java工具类
- 项目经验分享——Java常用工具类集合
- java中关于文件操作常用工具类
- Java多线程编程--(10)学习Java5.0 并发编程包--线程工具类
- 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途
- Java数组操作的常用工具类
- 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
- java并发库之Executors常用的创建ExecutorService的几个方法说明
- NO.63 [file]IO常用工具类IOUtils(Java读文件、写文件、打Zip包)
- [JAVA][StringUtils]字符串工具类的常用方法
- 【Java常用工具类】之log4j 一: log4j超快速入门
- 黑马程序员_Java中的两个常用工具类及集合数组的相互转换