java使用CountDownLatch实现线程顺序执行
2017-07-15 13:04
357 查看
package concurrent;
import java.util.concurrent.CountDownLatch;
public class ThreadSequence {
public static void main(String[] args) throws InterruptedException {
final CountDownLatch latch1 = new CountDownLatch(1);
Thread t1 = new Thread(new FirstThread(latch1));
final CountDownLatch latch2 = new CountDownLatch(1);
Thread t2 = new Thread(new SecondThread(latch2));
final CountDownLatch latch3 = new CountDownLatch(1);
Thread t3 = new Thread(new ThirdThread(latch3));
t1.start();
latch1.await();
t2.start();
latch2.await();
t3.start();
latch3.await();
System.out.println("the main thread is End...");
}
}
class FirstThread implements Runnable{
private final CountDownLatch latch;
public FirstThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the first thread is running!");
SleepUtils.sleep(5);
System.out.println("FirstThread----------->count=" + latch.getCount());
latch.countDown();
}
}
class SecondThread implements Runnable{
private final CountDownLatch latch;
public SecondThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the second thread is running!");
SleepUtils.sleep(2);
System.out.println("SecondThread----------->count=" + latch.getCount());
latch.countDown();
}
}
class ThirdThread implements Runnable{
private final CountDownLatch latch;
public ThirdThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the third thread is running!");
System.out.println("ThirdThread----------->count=" + latch.getCount());
latch.countDown();
}
}
import java.util.concurrent.CountDownLatch;
public class ThreadSequence {
public static void main(String[] args) throws InterruptedException {
final CountDownLatch latch1 = new CountDownLatch(1);
Thread t1 = new Thread(new FirstThread(latch1));
final CountDownLatch latch2 = new CountDownLatch(1);
Thread t2 = new Thread(new SecondThread(latch2));
final CountDownLatch latch3 = new CountDownLatch(1);
Thread t3 = new Thread(new ThirdThread(latch3));
t1.start();
latch1.await();
t2.start();
latch2.await();
t3.start();
latch3.await();
System.out.println("the main thread is End...");
}
}
class FirstThread implements Runnable{
private final CountDownLatch latch;
public FirstThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the first thread is running!");
SleepUtils.sleep(5);
System.out.println("FirstThread----------->count=" + latch.getCount());
latch.countDown();
}
}
class SecondThread implements Runnable{
private final CountDownLatch latch;
public SecondThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the second thread is running!");
SleepUtils.sleep(2);
System.out.println("SecondThread----------->count=" + latch.getCount());
latch.countDown();
}
}
class ThirdThread implements Runnable{
private final CountDownLatch latch;
public ThirdThread(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println("the third thread is running!");
System.out.println("ThirdThread----------->count=" + latch.getCount());
latch.countDown();
}
}
相关文章推荐
- java使用CountDownLatch实现线程顺序执行
- Java 多线程 (PART XIX) 使用Condition实现线程的顺序执行
- java多线程-使用ReentrantLock实现线程的顺序执行
- 使用 CountDownLatch 控制多个线程执行顺序
- java 中的wait和notify以及synchronized的使用,实现两个线程交替执行
- Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
- 使用condition实现顺序执行线程
- 使用信号量来实现线程的先后执行顺序
- Java中使用ReentrantLock控制线程执行顺序
- 使用java.util.concurrent.ExecutorService实现Java线程异步执行
- 使用 CountDownLatch 控制多个线程执行顺序
- 使用 CountDownLatch 控制多个线程执行顺序
- 三个线程轮流执行顺序打印ABC(三):使用Lock实现
- 三个线程轮流执行顺序打印ABC(二):使用Lock+Condition实现
- Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
- C#之使用AutoResetEvent实现线程的顺序执行
- 三个线程轮流执行顺序打印ABC(一):使用Semaphore实现
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- Java 多线程 (PART VII) 使用join()方法保证线程的执行顺序
- java基础问题---java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用