码迷,mamicode.com
首页 > 编程语言 > 详细

多线程 实际场景应用篇

时间:2018-02-03 23:11:49      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:string   周末   class   span   color   star   current   over   number   

以下示例均来自jdk concurrent包。
/**
* 有一个任务,它要等待其他几个任务
* 执行完毕 之后才能执行
*
*
* 倒计时器
*/
public class CountDownLatchTest {



public static void main(String[] args){
final CountDownLatch count = new CountDownLatch(2);
new Thread(){
public void run(){
try {
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("线程"+Thread.currentThread().getName()+"正在执行...");
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("线程"+Thread.currentThread().getName()+"执行完毕 ");
count.countDown();

} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();



//任务2
new Thread(){
public void run(){
try {
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("线程"+Thread.currentThread().getName()+"正在执行...");
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("线程"+Thread.currentThread().getName()+"执行完毕 ");
count.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();

try {
count.await();
System.out.println("主线程"+Thread.currentThread().getName()+"执行完毕 ");
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}

/**
* 周末部门聚餐
* 所有人到齐了 之后 才能开始。。。
*
*
* 可循环的同步屏障 */
public class CyclicBarrierTest {


public static void main(String[] args){
final CyclicBarrier barrier = new CyclicBarrier(20);
ExecutorService threadPool = Executors.newCachedThreadPool();
//模拟20个用户
for(int i=1;i<=20;i++){
final int t=i;
Runnable r = new Runnable(){
public void run(){
//模拟每个人来的时间
try {
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println(t+" 用户到达"+(barrier.getNumberWaiting()+1)+"人到达");
barrier.await();
if(t==1){ //控制打印一条
System.out.println("人员全部到齐,开始吃饭");
}
Thread.sleep(300);
System.out.println(t+"用户吃完,可以回家 了");
} catch (Exception e) {
e.printStackTrace();
}
}
};
threadPool.execute(r);
}

threadPool.shutdown();
}
}

/**
* 团伙a 绑了B,放言要100万, 与家属c达到 一致意见在某个地方
* 交换人质,于是Ac同时来到交换地点,同时一手交钱一手交货
* 使用exchanger实现
*/
public class ExchangerTest {

public static void main(String[] args){
ExecutorService threadPool = Executors.newCachedThreadPool();
Exchanger<String> exchanger = new Exchanger<String>();

threadPool.execute(new Runnable() {
@Override
public void run() {
String data ="人质B";
try {
String response = exchanger.exchange(data);
System.out.println("绑架者用B换回"+response);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

//家属c
threadPool.execute(new Runnable() {
@Override
public void run() {
String data ="1000";
try {
String response = exchanger.exchange(data);
System.out.println("C1000万换回"+response);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

threadPool.shutdown();
}
}

/**
* 信号量控制并发
* 20个人 2个窗口买票
*
*/
public class SemaphoreTest {
private void execute(){
Semaphore semaphore = new Semaphore(2);
//线程池
ExecutorService threadPool = Executors.newCachedThreadPool();
//模拟20个线程
for(int i=0;i<20;i++) {
threadPool.execute(new MyTask(semaphore,i));
}
}

class MyTask implements Runnable{
private Semaphore semaphore; //信号 量 2个许可
private int userNo;
public MyTask(Semaphore semaphore,int userNo){
this.semaphore=semaphore;
this.userNo =userNo;
}
public void run(){
//执行买票

try {
//获取信号量许可,如果获取不到 在此阻塞
semaphore.acquire();

//如果 获取到, 可以往下执行 也就是买票
System.out.println("用户"+userNo+" 进入窗口,准备买票");
TimeUnit.MILLISECONDS.sleep(1000);

System.out.println("用户"+userNo+" 买票完成,准备离开 ");
TimeUnit.MILLISECONDS.sleep(1000);
System.out.println("用户"+userNo+" 买票完成,离开窗口 ");
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
SemaphoreTest test = new SemaphoreTest();
test.execute();

}
}
 
 
 

多线程 实际场景应用篇

标签:string   周末   class   span   color   star   current   over   number   

原文地址:https://www.cnblogs.com/xifenglou/p/8410784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!