控制多線程執行順序
阿新 • • 發佈:2018-12-01
執行 ade The () 繼續 code 兩種 執行順序 等待
雖然項目用不上,先備份吧,控制多線程執行順序有兩種方法
1.通過join方法保證多線程的順序性的特性
join:讓主線程等待子線程結束後才能繼續執行
public static void main(String[] args) throws InterrupterException { thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.star(); }
2.
ExcutorService executor = Excutors.newSingleTheadExcutor():FIFO
static ExcutorService excutorService = Excutors.newSingleTheadExcutor(); public static void main(String[] args) throws InterrupterException { excutorService.submit(thread1); excutorService.submit(thread2); excutorService.submit(thread3); }
控制多線程執行順序