控制多线程执行顺序

虽然项目用不上,先备份吧,控制多线程执行顺序有两种方法

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);
}

 

posted @ 2018-12-01 12:18  大腮鱼  阅读(257)  评论(0编辑  收藏  举报