现有T1T2T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了现有T1T2T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?相关的知识,希望对你有一定的参考价值。
考察join
/** * 现有T1、T2、T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行? * @author user * */ public class Test3 { public static void main(String[] args) throws InterruptedException { Thread T1 = new Thread(new T1()); Thread T2 = new Thread(new T2()); Thread T3 = new Thread(new T3()); T1.start(); T1.join(); T2.start(); T2.join(); T3.start(); T3.join(); } } class T1 implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread()+"T1"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } class T2 implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread()+"T2"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } class T3 implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread()+"T3"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
本文出自 “12212886” 博客,请务必保留此出处http://12222886.blog.51cto.com/12212886/1964196
以上是关于现有T1T2T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?的主要内容,如果未能解决你的问题,请参考以下文章