多线程 实际场景应用篇

以下示例均来自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();

}
}
 
 
 
posted @ 2018-02-03 21:20  西凤楼  阅读(800)  评论(0编辑  收藏  举报
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。 感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。 因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询!