如何控制多线程的执行顺序?
方法一:使用join()方法让一个线程强制运行
调用一个线程的join()方法就可以让这个线程强制运行,并且它会阻塞主线程的运行。
原理:调用join方法,会调用join(0)方法,当参数为0时,会调用wait方法,使主线程阻塞,等待子线程执行完毕后,主线程结束等待,继续执行。
wait()的作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。
public static void main(String[] args) {
try {
System.out.println("main开始运行");
thread1.start();
thread1.join(); //让thread1强制执行完毕后,才可以执行后面的代码
thread2.start();
thread2.join();
thread3.start();
thread3.join();
System.out.println("main运行结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
方法二:使用juc包下的Executors线程池保证子线程按顺序执行
使用Executors中的newSingleThreadExecutor()方法,创建一个单线程的线程池,
将线程用排队的方式扔进一个线程池里,让所有的任务以单线程的模式,按照FIFO先进先出,也可以达到控制线程执行顺序的目的。
static ExecutorService executorService =
Executors.newSingleThreadExecutor();
public static void main(String[] args) {
System.out.println("main开始运行");
executorService.submit(thread1);
executorService.submit(thread2);
executorService.submit(thread3);
executorService.shutdown();
System.out.println("main运行结束");
}
方法三:用CountdownLach保证子线程按顺序执行
利用CountdownLach可以使一个线程等待其他线程完成某件事情之后才能执行,
它的内部提供了一个计数器,在构造CountdownLach时必须指定计数器的初始值,且计数器的初始值必须>=0。另外它还提供了一个countDown方法来操作计数器的值,每调用一次countDown方法计数器都会减1,直到计数器的值减为0时,所有因调用await方法而阻塞的线程都会被唤醒。这就是CountDownLatch的内部机制。
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) {
}
}
}
}
方法四:用BlockingQueue
将线程放入到BlockingQueue中,依次取出BlockingQueue中的线程,执行线程的start方法。
阻塞队列 (BlockingQueue)是Java util.concurrent包下重要的数据结构,BlockingQueue提供了线程安全的队列访问方式:当阻塞队列进行插入数据时,如果队列已满,线程将会阻塞等待直到队列非满;从阻塞队列取数据时,如果队列已空,线程将会阻塞等待直到队列非空。并发包下很多高级同步类的实现都是基于BlockingQueue实现的。
public class ThreadTest4 {
// T1、T2、T3三个线程顺序执行
public static void main(String[] args) {
//blockingQueue保证顺序
BlockingQueue<Thread> blockingQueue = new LinkedBlockingQueue<Thread>();
Thread t1 = new Thread(new Work());
Thread t2 = new Thread(new Work());
Thread t3 = new Thread(new Work());
blockingQueue.add(t1);
blockingQueue.add(t2);
blockingQueue.add(t3);
for (int i=0;i<3;i++) {
Thread t = null;
try {
t = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
t.start();
//检测线程是否还活着
while (t.isAlive());
}
}
static class Work implements Runnable {
public void run() {
System.out.println("thread start:" + Thread.currentThread().getName());
}
}
}