如何保證執行緒按順序執行
阿新 • • 發佈:2019-01-03
/* 有三個執行緒T1 T2 T3,如何保證他們按順序執行-轉載 在T2的run中,呼叫t1.join,讓t1執行完成後再讓T2執行 在T2的run中,呼叫t2.join,讓t2執行完成後再讓T3執行 */ public class ThreadByOrder { static Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println("t1"); } }); static Thread t2 = newThread(new Runnable() { @Override public void run() { try { t1.join(); Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("t2"); } }); static Thread t3 = new Thread(new Runnable() { @Override public void run() { try { t2.join(); Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch blocke.printStackTrace(); } System.out.println("t3"); } }); public static void main(String[] args) { t1.start(); t2.start(); t3.start(); } }