java同时运行多个线程_java多个线程同时开始执行

有时候有这样的需求,需要多个线程执行任务,每个线程都执行不同的准备工作,全部准备好了,才能同时执行下面的工作。这个怎么实现呢?

一是用CyclicBarrier (适用在多线程相互等待,直到到达一个屏障点。并且CyclicBarrier是可重用的。)实现。

代码实现:

public class CyclicBarrierTest {

boolean finalFlg = false;

/**

* CyclicBarrier 适用再多线程相互等待,直到到达一个屏障点。并且CyclicBarrier是可重用的。

*/

CyclicBarrier cyclicBarrier = new CyclicBarrier(10);

private void runThread() {

ExecutorService executorService = Executors.newFixedThreadPool(10);

for (int i = 0; i < 10; i++) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

executorService.submit(createThread(i));

}

}

private Thread createThread(int i) {

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

try {

cyclicBarrier.await();

System.out.println("Thread:" + Thread.currentThread().getName() + "准备完毕,time:" + System.currentTimeMillis());

} catch (InterruptedException e) {

e.printStackTrace();

} catch (BrokenBarrierException e) {

e.printStackTrace();

}

}

});

thread.setName("name" + i);

return thread;

}

public static void main(String[] args) {

CyclicBarrierTest test = new CyclicBarrierTest();

test.runThread();

}

}

执行结果:

Thread:pool-1-thread-10准备完毕,time:1521037864652

Thread:pool-1-thread-1准备完毕,time:1521037864652

Thread:pool-1-thread-3准备完毕,time:1521037864652

Thread:pool-1-thread-2准备完毕,time:1521037864652

Thread:pool-1-thread-7准备完毕,time:1521037864652

Thread:pool-1-thread-6准备完毕,time:1521037864652

Thread:pool-1-thread-5准备完毕,time:1521037864652

Thread:pool-1-thread-4准备完毕,time:1521037864652

Thread:pool-1-thread-9准备完毕,time:1521037864652

Thread:pool-1-thread-8准备完毕,time:1521037864652

从结果看,线程几乎是同时执行的

二是用CountDownLatch 实现,CountDownLatch可以理解为一个计数器,直到计数器为0了,等待的线程才执行。

public class testCountDownLatch {

CountDownLatch countDownLatch = new CountDownLatch(1);

private void runThread(){

ExecutorService executorService = Executors.newFixedThreadPool(10);

for(int i=0;i<10 ;i++){

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

executorService.submit(new Runnable() {

@Override

public void run() {

try {

countDownLatch.await();

System.out.println("Thread:"+Thread.currentThread().getName()+",time: "+System.currentTimeMillis());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

});

}

countDownLatch.countDown();

}

public static void main(String[] args) {

testCountDownLatch test = new testCountDownLatch();

test.runThread();

}

}

从执行结果看,线程也几乎同时进行的:

Thread:pool-1-thread-2,time: 1521038129807

Thread:pool-1-thread-3,time: 1521038129807

Thread:pool-1-thread-1,time: 1521038129807

Thread:pool-1-thread-4,time: 1521038129807

Thread:pool-1-thread-6,time: 1521038129807

Thread:pool-1-thread-5,time: 1521038129807

Thread:pool-1-thread-9,time: 1521038129807

Thread:pool-1-thread-8,time: 1521038129807

Thread:pool-1-thread-7,time: 1521038129807

Thread:pool-1-thread-10,time: 1521038129807

其他自己用wait和notify实现。

下一篇介绍下CyclicBarrier的用法。

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页