Java模拟并发
=========================one=============================
public class Bingfa {
public static void main(String[] args) throws InterruptedException {
// 锁住所有线程,等待并发执行
final CountDownLatch begin = new CountDownLatch(1);
final ExecutorService exec = Executors.newFixedThreadPool(10);
for (int index = 0; index < 100; index++)
{
final int NO = index + 1;
Runnable run = new Runnable()
{
public void run() {
try {
// 等待,所有一起执行
begin.await();
//*****执行程序去********//
????????????
//*****执行程序去********//
} catch (InterruptedException e)
{
e.printStackTrace();
}
finally {
}
}
};
exec.submit(run);
}
System.out.println("开始执行");
// begin减一,开始并发执行
begin.countDown();
//关闭执行
exec.shutdown();
}
}
==========================two==================================
public class CountdownLatchTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool(); //创建一个线程池
final CountDownLatch cdOrder = new CountDownLatch(1);//构造方法参数指定计数的次数
final CountDownLatch cdAnswer = new CountDownLatch(3);//构造方法参数指定计数的次数
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
try {
System.out.println("线程" + Thread.currentThread().getName() +
"正准备接受命令");
cdOrder.await(); //战士们都处于等待命令状态
System.out.println("线程" + Thread.currentThread().getName() +
"已接受命令");
Thread.sleep((long)(Math.random()*10000));
System.out.println("线程" + Thread.currentThread().getName() +
"回应命令处理结果");
} catch (Exception e) {
e.printStackTrace();
} finally {
cdAnswer.countDown(); //任务执行完毕,返回给指挥官,cdAnswer减1。
}
}
};
service.execute(runnable);//为线程池添加任务
}
try {
Thread.sleep((long)(Math.random()*10000));
System.out.println("线程" + Thread.currentThread().getName() +
"即将发布命令");
cdOrder.countDown(); //发送命令,cdOrder减1,处于等待的战士们停止等待转去执行任务。
System.out.println("线程" + Thread.currentThread().getName() +
"已发送命令,正在等待结果");
cdAnswer.await(); //命令发送后指挥官处于等待状态,一旦cdAnswer为0时停止等待继续往下执行
System.out.println("线程" + Thread.currentThread().getName() +
"已收到所有响应结果");
} catch (Exception e) {
e.printStackTrace();
} finally {
}
service.shutdown(); //任务结束,停止线程池的所有线程
}
}
转载:http://blog.csdn.net/zhao9tian/article/details/40346899