如何让多线程按顺序执行,让线程按照顺序执行的8种方法
00-1010顺序由子线程中的join()方法指定,顺序由主线程中的join()方法通过倒计时定时器CountDownLatch指定。文章介绍了四种方法,简单易懂,可以通过四个演示来介绍。
00-1010通过join()方法使当前线程“阻塞”,等待指定线程执行完毕再继续。比如在线程thread2上加一句thread1.join(),意思是当当前线程thread2运行到这行代码时,会进入阻塞状态,线程thread2直到线程thread1执行完才会继续运行,这样就保证了线程thread1和线程thread2的运行顺序。
public class ThreadJoinDemo { public static void main(String[]args)Thread interrupted exception { final Thread Thread 1=new Thread(new Runnable(){ @ override public void run(){ system . out . println(打开冰箱!);} });最终线程Thread 2=new Thread(new Runnable(){ @ Override public void run(){ try { Thread 1 . join();} catch(interrupted exception e){ e . printstacktrace();} System.out.println(拿出一瓶牛奶!);} });最终线程Thread 3=new Thread(new Runnable(){ @ Override public void run(){ try { Thread 2 . join();} catch(interrupted exception e){ e . printstacktrace();} System.out.println(关上冰箱!);} });//下面三行代码的顺序可以随意调整,程序的运行结果不受影响,因为我们已经通过“join()方法”在子线程中指定了运行顺序。thread 3 . start();thread 2 . start();thread 1 . start();}}运行结果:
打开冰箱!拿出一瓶牛奶!关上冰箱!
00-1010下面简单说一下子线程和主线程的区别。子线程是指发生在线程内部的代码,主线程是指发生在主函数中的代码。我们可以通过join()方法使主线程阻塞并在主函数中等待,达到按指定顺序执行的目的。
公共类ThreadMainJoinDemo { public static void main(String[]args)抛出中断的异常{ final thread thread 1=new thread(new runnable(){ @ override public void run(){ system . out . println(打开冰箱!);} });final thread 2=new thread(newrunnable(){ @ override public void run(){ system . out . println(拿出一瓶牛奶!);} });最终推力
ead thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println("关上冰箱!"); } }); thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.start(); }}输出结果:
打开冰箱!拿出一瓶牛奶!关上冰箱!
通过倒数计时器CountDownLatch实现
CountDownLatch通过计数器提供了更灵活的控制,只要检测到计数器为0当前线程就可以往下执行而不用管相应的thread是否执行完毕。
public class ThreadCountDownLatchDemo { private static CountDownLatch countDownLatch1 = new CountDownLatch(1); private static CountDownLatch countDownLatch2 = new CountDownLatch(1); public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("打开冰箱!"); countDownLatch1.countDown(); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch1.await(); System.out.println("拿出一瓶牛奶!"); countDownLatch2.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch2.await(); System.out.println("关上冰箱!"); } catch (InterruptedException e) { e.printStackTrace(); } } }); //下面三行代码顺序可随意调整,程序运行结果不受影响 thread3.start(); thread1.start(); thread2.start(); }}
输出结果:
打开冰箱!拿出一瓶牛奶!关上冰箱!
通过创建单一化线程池newSingleThreadExecutor()实现
单线程化线程池(newSingleThreadExecutor)的优点,串行执行所有任务。
public class ThreadPoolDemo { static ExecutorService executorService = Executors.newSingleThreadExecutor(); public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("打开冰箱!"); } }); final Thread thread2 =new Thread(new Runnable() { @Override public void run() { System.out.println("拿出一瓶牛奶!"); } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println("关上冰箱!"); } }); executorService.submit(thread1); executorService.submit(thread2); executorService.submit(thread3); executorService.shutdown(); //使用完毕记得关闭线程池 } }
输出结果:
打开冰箱!拿出一瓶牛奶!关上冰箱!
到此这篇关于Java让多线程按顺序执行的几种方法的文章就介绍到这了,更多相关Java多线程按顺序执行内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。