有三个线程T1,T2,T3,如何保证顺序执行?
1.使用join
在多线程中有多种方法让线程按特定顺序执行,可以用线程类的join()方法在一个线程中启动另一 个线程,另外一个线程完成该线程继续执行。
为了确保三个线程的顺序你应该先启动最后一个(T3调 用T2,T2调用T1),这样T1就会先完成而T3最后完成。
实际上先启动三个线程中哪一个都行, 因为在每个线程的run方法中用join方法限定了三个线程的 执行顺序。
public class JoinTest2 {
// 1.现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行 public static void main(String[] args) {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1");
}
});
final Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
// 引用t1线程,等待t1线程执行完
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("t2");
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
// 引用t2线程,等待t2线程执行完
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3");
}});
t3.start();
//这里三个线程的启动顺序可以任意,大家可以试下!
t2.start(); t1.start();
}
}
2.使用CountDownLatch
CountDownLatch(闭锁)是一个很有用的工具类,利用它我们可以拦截一个或多个线程使其在某个条件成熟后再执行。它的内部提供了一个计数器,在构造闭锁时必须指定计数器的初始值,且计数器的初始值必须大于0。
public class ThreadTest2 {
// T1、T2、T3三个线程顺序执行
public static void main(String[] args) {
CountDownLatch c0 = new CountDownLatch(0); //计数器为0
CountDownLatch c1 = new CountDownLatch(1); //计数器为1
CountDownLatch c2 = new CountDownLatch(1); //计数器为1
Thread t1 = new Thread(new Work(c0, c1));
//c0为0,t1可以执行。t1的计数器减1
Thread t2 = new Thread(new Work(c1, c2));
//t1的计数器为0时,t2才能执行。t2的计数器c2减1
Thread t3 = new Thread(new Work(c2, c2));
//t2的计数器c2为0时,t3才能执行
t1.start();
t2.start();
t3.start();
}
//定义Work线程类,需要传入开始和结束的CountDownLatch参数
static class Work implements Runnable {
CountDownLatch c1;
CountDownLatch c2;
Work(CountDownLatch c1, CountDownLatch c2) {
super();
this.c1 = c1;
this.c2 = c2;
}
public void run() {
try {
c1.await();//前一线程为0才可以执行
System.out.println("thread start:" + Thread.currentThread().getName());
c2.countDown();//本线程计数器减少
} catch (InterruptedException e) {
}
}
}
}
3.CachedThreadPool
FutureTask一个可取消的异步计算,FutureTask 实现了Future的基本方法,提空 start cancel 操作,可以查询计算是否已经完成,并且可以获取计算的结果。结果只可以在计算完成之后获取,get方法会阻塞当计算没有完成的时候,一旦计算已经完成,那么计算就不能再次启动或是取消。
public class ThreadTest3 {
// T1、T2、T3三个线程顺序执行
public static void main(String[] args) {
FutureTask<Integer> future1= new FutureTask<Integer>(new Work(null));
Thread t1 = new Thread(future1);
FutureTask<Integer> future2= new FutureTask<Integer>(new Work(future1));
Thread t2 = new Thread(future2);
FutureTask<Integer> future3= new FutureTask<Integer>(new Work(future2));
Thread t3 = new Thread(future3);
t1.start();
t2.start();
t3.start();
}
static class Work implements Callable<Integer> {
private FutureTask<Integer> beforeFutureTask;
public Work(FutureTask<Integer> beforeFutureTask) {
this.beforeFutureTask = beforeFutureTask;
}
public Integer call() throws Exception {
if (beforeFutureTask != null) {
Integer result = beforeFutureTask.get();//阻塞等待
System.out.println("thread start:" + Thread.currentThread().getName());
} else {
System.out.println("thread start:" + Thread.currentThread().getName());
}
return 0;
}
}
}
4.使用单个线程池
newSingleThreadExecutor返回以个包含单线程的Executor,将多个任务交给此Exector时,这个线程处理完一个任务后接着处理下一个任务,若该线程出现异常,将会有一个新的线程来替代。
public class ThreadTest5 {
public static void main(String[] args) throws InterruptedException {
final Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " run 1");
}
}, "T1");
final Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " run 2");
try {
t1.join(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T2");
final Thread t3 = new Thread(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " run 3");
try {
t2.join(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "T3");
//使用 单个任务的线程池来实现。保证线程的依次执行
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(t1);
executor.submit(t2);
executor.submit(t3);
executor.shutdown();
}
}