Semaphore 类 限制同事处理的进程数量
package duoxiancheng;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* Created by Administrator on 2017/12/14.
* 排队买票 同时进行的进程数量
*/
public class SemaphoreDemo {
class SemaphoreRunable implements Runnable{
private Semaphore semaphore;//信号量
private int user;//记录第几个用户
public SemaphoreRunable(Semaphore semaphore,int user){
this.semaphore=semaphore;
this.user=user;
}
public void run() {
try {
//获取型号量许可
semaphore.acquire();
System.out.println("用户【"+user+"】进入窗口,准备买票");
Thread.sleep((long)(Math.random()*10000));
System.out.println("用户【"+user+"】买票完成,即将离开...");
Thread.sleep((long)(Math.random()*10000));
System.out.println("用户【"+user+"】离开售票窗口...");
//释放
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void execute(){
//定义窗口个数
final Semaphore semaphore=new Semaphore(2); //信号量 控制并发线程数
//线程池
ExecutorService threadPool= Executors.newCachedThreadPool();
//模拟20个用户
for (int i=0;i<20;i++){
//执行买票
threadPool.execute(new SemaphoreRunable(semaphore,(i+1)));
}
}
public static void main(String[] args){
SemaphoreDemo sd=new SemaphoreDemo();
sd.execute();
}
}
CountDownLatch类 等待指定数目的进程运行结束 统一进行下一步操作
package duoxiancheng;
import java.util.concurrent.CountDownLatch;
/**
* 等待指定线程数量执行完,之后后一起进行其他操作
* @author Administrator
*
*/
public class CountDownLatchDemo {
public static void main(String[] args) {
final CountDownLatch latch= new CountDownLatch(2);
//任务1
new Thread(){
public void run() {
try {
System.out.println("任务1正在执行任务...");
Thread.sleep((long)(Math.random()*10000));
System.out.println("任务1执行完毕...");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
//任务2
new Thread(){
public void run() {
try {
System.out.println("任务2正在执行任务...");
Thread.sleep((long)(Math.random()*10000));
System.out.println("任务2执行完毕...");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
//主线程
System.out.println("等待其他两个任务执行完毕,主线程才开始执行:"+Thread.currentThread().getName());
try {
latch.await();//同步点 阻塞点
System.out.println("两个任务执行完毕:"+Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 任务1正在执行任务...
等待其他两个任务执行完毕,主线程才开始执行:main
任务2正在执行任务...
任务1执行完毕...
任务2执行完毕...
两个任务执行完毕:main
*/
CyclicBarrier 统一处理结果
package duoxiancheng;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Administrator on 2017/12/14.
* CyclicBarrier 可循环的障碍物
* 多线程计算之后,最后合并结果
*/
public class CyclicBarrierDemo {
public static void main(String[] args){
//3个人聚餐
final CyclicBarrier cb =new CyclicBarrier(3, new Runnable() {
public void run() {
System.out.println("人员全部到齐了,拍照留念。。。");
try {
Thread.sleep((long)(Math.random()*10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
//线程池
ExecutorService threadPool= Executors.newCachedThreadPool();
//模拟3个用户
for (int i=0;i<3;i++) {
final int user =i+1;
Runnable r=new Runnable() {
public void run() {
//模拟每个人来的时间不一样
try {
Thread.sleep((long)(Math.random()*10000));
System.out.println(user+"到达聚餐点,当前已有"+(cb.getNumberWaiting()+1)+"人达到");
//阻塞
cb.await();
if(user==1){ //打印一句
System.out.println("拍照结束,开始吃饭...");
}
Thread.sleep((long)(Math.random()*10000));
System.out.println(user+"吃完饭..准备回家.");
}catch (InterruptedException e){
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
};
threadPool.execute(r);
}
threadPool.shutdown();
}
}
/*
1到达聚餐点,当前已有1人达到
2到达聚餐点,当前已有2人达到
3到达聚餐点,当前已有3人达到
人员全部到齐了,拍照留念。。。
拍照结束,开始吃饭...
1吃完饭..准备回家.
2吃完饭..准备回家.
3吃完饭..准备回家.
*/
Exchanger 交换器
package duoxiancheng;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Administrator on 2017/12/14.
*/
public class ExchangerDemo {
public static void main(String[] args){
//交换器,交换String类型
Exchanger<String> ec =new Exchanger<String>();
//线程池
ExecutorService threadPool = Executors.newCachedThreadPool();
//张三团伙
threadPool.execute(new Runnable() {
public void run() {
try {
String returnStr = ec.exchange("小青蛙");
System.out.println("绑架者用小青蛙交换回: "+returnStr);
}catch (InterruptedException e){
e.printStackTrace();
}
}
});
//大桥
threadPool.execute(new Runnable() {
public void run() {
try {
String returnStr = ec.exchange("1000万");
System.out.println("大桥用1000万交换回: "+returnStr);
}catch (InterruptedException e){
e.printStackTrace();
}
}
});
threadPool.shutdown();
}
}
/*
* 绑架者用小青蛙交换回: 1000万
大桥用1000万交换回: 小青蛙
*/