博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA线程14 - 新特性:同步工具
阅读量:5746 次
发布时间:2019-06-18

本文共 7054 字,大约阅读时间需要 23 分钟。

hot3.png

一、Semaphore

1. 简介

Semaphore实现信号量。

Semaphore可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制同时访问资源的线程个数。例如:实现一个文件允许的并发访问数。

单个信号量的Semaphore对象可以实现互斥锁的功能,并且可以是由一个线程获得了锁,再由另一个线程释放锁。这可应用于死锁恢复的一些场合。

2. 示例

import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;public class SemaphoreTest {    public static void main(String[] args) {        ExecutorService es = Executors.newCachedThreadPool();        final Semaphore sp = new Semaphore(3);        for (int i = 0; i < 10; i++) {            Runnable r = new Runnable() {                @Override                public void run() {                    try {                        sp.acquire();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("线程"+Thread.currentThread().getName()+"进入,当前已有并发"+(3-sp.availablePermits()));                    try {                        Thread.sleep(new Random().nextInt(10000));                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("线程"+Thread.currentThread().getName()+"即将离开");                    sp.release();                    System.out.println("线程"+Thread.currentThread().getName()+"已离开,当前已有并发"+(3-sp.availablePermits()));                }            };            es.execute(r);        }        es.shutdown();    }}

二、CyclicBarrier

1. 简介

障碍器。表示大家彼此等待,大家集合好后才开始出发,分散活动后又在指定地点集合碰面。

2. 示例

import java.util.Date;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CyclicBarrierTest {    public static void main(String[] args) {        ExecutorService es = Executors.newCachedThreadPool();        final CyclicBarrier cb = new CyclicBarrier(5);        for (int i = 0; i < 5; i++) {            Runnable r = new Runnable() {                @Override                public void run() {                    try {                        Thread.sleep(new Random().nextInt(10000));                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"到达目的地1,当前已到达"+(cb.getNumberWaiting()+1)+",正在等候");                        cb.await();                        System.out.println(new Date()+":所有线程已到达,向目的地2出发");						                        Thread.sleep(new Random().nextInt(10000));                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"到达目的地2,当前已到达"+(cb.getNumberWaiting()+1)+",正在等候");                        cb.await();                        System.out.println(new Date()+":所有线程已到达,向目的地3出发");						                        Thread.sleep(new Random().nextInt(10000));                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"到达目的地3,当前已到达"+(cb.getNumberWaiting()+1)+",正在等候");                        cb.await();                        System.out.println(new Date()+":所有线程已到达,任务结束");                    } catch (InterruptedException e) {                        e.printStackTrace();                    } catch (BrokenBarrierException e) {                        e.printStackTrace();                    }                }            };            es.execute(r);        }        es.shutdown();    }}

三、CountDownLatch

1. 简介

犹如倒计时计数器,调用CountDownLatch对象的countDown()方法就将计数器-1,当计数器到达0时,则所有等待者或者单个等待着开始执行。

2. 示例

import java.util.Date;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CountDownLatchTest {    public static void main(String[] args) {        ExecutorService es = Executors.newCachedThreadPool();        final CountDownLatch cdOrder = new CountDownLatch(1);        final CountDownLatch cdAnswer = new CountDownLatch(5);        for (int i = 0; i < 5; i++) {            Runnable r = new Runnable() {                @Override                public void run() {                    try {                        //Thread.sleep(new Random().nextInt(10000));                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"准备接受命令");                        cdOrder.await();                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"已接受命令");                        Thread.sleep(new Random().nextInt(10000));                        System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"处理命令");                        cdAnswer.countDown();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            };            es.execute(r);        }		        try {            Thread.sleep(new Random().nextInt(10000));            System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"即将发布命令");            cdOrder.countDown();            System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"已发布命令,正等待结果");            cdAnswer.await();            System.out.println(new Date()+":线程"+Thread.currentThread().getName()+"已收到所有响应结果");        } catch (Exception e) {            e.printStackTrace();        }		        es.shutdown();    }}

四、Exchanger

1. 简介

Exchanger用于两个线程之间的数据交换。每个线程在完成一定任务后想与对方交换数据,第一个先拿出数据的线程将一直等待第二个线程拿着数据的到来,才能彼此交换数据。

2. 示例

import java.util.Date;import java.util.Random;import java.util.concurrent.Exchanger;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Exchanger交换机 两个线程之间用户交换数据 */public class ExchangerTest {    public static void main(String[] args) {        ExecutorService service = Executors.newCachedThreadPool();        // 线程之间交换数据        final Exchanger exchanger = new Exchanger();        service.execute(new Runnable() {            public void run() {                try {                    String data = "【数据1】";                    System.out.println(new Date()+":"+Thread.currentThread().getName() + "准备把" + data + "换出去");                    Thread.sleep(new Random().nextInt(10000));                    String data2 = (String) exchanger.exchange(data);                    System.out.println(new Date()+":"+Thread.currentThread().getName() + "换回的数据是" + data2);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        });        service.execute(new Runnable() {            public void run() {                try {                    String data = "【数据2】";                    System.out.println(new Date()+":"+Thread.currentThread().getName() + "准备把" + data + "换出去");                    Thread.sleep(new Random().nextInt(10000));                    String data2 = (String) exchanger.exchange(data);                    System.out.println(new Date()+":"+Thread.currentThread().getName() + "换回的数据是" + data2);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }}

转载于:https://my.oschina.net/hongdengyan/blog/205417

你可能感兴趣的文章
【云栖大会】探索商业升级之路
查看>>
HybridDB实例新购指南
查看>>
C语言及程序设计提高例程-35 使用指针操作二维数组
查看>>
华大基因BGI Online的云计算实践
查看>>
深入理解自定义Annotation,实现ButterKnif小原理
查看>>
排序高级之交换排序_冒泡排序
查看>>
Cocos2d-x3.2 Ease加速度
查看>>
[EntLib]关于SR.Strings的使用办法[加了下载地址]
查看>>
中小型网站架构分析及优化
查看>>
写shell的事情
查看>>
负载均衡之Haproxy配置详解(及httpd配置)
查看>>
linux虚拟机拷贝之后联网出错
查看>>
Linux文件系统探索
查看>>
标准与扩展ACL 、 命名ACL 、 总结和答疑
查看>>
查找恶意的TOR中继节点
查看>>
MAVEN 属性定义与使用
查看>>
hadoop2.7.2 HA搭建
查看>>
shell高级视频答学生while循环问题
查看>>
使用@media实现IE hack的方法
查看>>
《11招玩转网络安全》之第一招:Docker For Docker
查看>>