保证三个线程依次按顺序执行
2017-12-11 15:14
567 查看
保证三个线程依次按顺序执行
废话不多说,下面贴代码final Thread t1 = new Thread(new Runnable() { public void run() { System.out.println(Thread.currentThread().getName() + " run 1"); } }, "T1"); final Thread t2 = new Thread(new Runnable() { public void run() { System.out.println(Thread.currentThread().getName() + " run 2"); try { t1.join(10); } catch (InterruptedException e) { e.printStackTrace(); } } }, "T2"); final Thread t3 = new Thread(new Runnable() { public void run() { System.out.println(Thread.currentThread().getName() + " run 3"); try { t2.join(10); } catch (InterruptedException e) { e.printStackTrace(); } } }, "T3"); t1.start(); t2.start(); t3.start(); }在这里我在t2的线程里面t1 的join方法,抢占时间片,然后在t3的线程里面,抢占t2的时间片,理应是 t3执行>t2执行>t1执行,可以到最后结果的时候,每次的结果都不尽相同,这让我很纳闷,有兴趣的同学可以尝试一下。欢迎互相讨论。在这里我就去翻看线程的文档,发现了newSingleThreadExecutor 这个线程池,保证线程里面的任务依次执行,很兴奋public class TestJoin {public static void main(String[] args) throws InterruptedException {final Thread t1 = new Thread(new Runnable() {public void run() {System.out.println(Thread.currentThread().getName() + " run 1");}}, "T1");final Thread t2 = new Thread(new Runnable() {public void run() {System.out.println(Thread.currentThread().getName() + " run 2");try {t1.join(10);} catch (InterruptedException e) {e.printStackTrace();}}}, "T2");final Thread t3 = new Thread(new Runnable() {public void run() {System.out.println(Thread.currentThread().getName() + " run 3");try {t2.join(10);} catch (InterruptedException e) {e.printStackTrace();}}}, "T3");// method1//t1.start();//t2.start();//t3.start();// method 2 使用 单个任务的线程池来实现。保证线程的依次执行ExecutorService executor = Executors.newSingleThreadExecutor();executor.submit(t1);executor.submit(t2);executor.submit(t3);executor.shutdown();}}在这里发现结果每次都是 t1执行,t2执行,t3执行,这里达到目的了之后,不禁回想,这个Single线程池为啥这么好用,不由得就像把线程池狠狠的看一遍,然后就去翻看了源代码
/*** Creates an Executor that uses a single worker thread operating* off an unbounded queue. (Note however that if this single* thread terminates due to a failure during execution prior to* shutdown, a new one will take its place if needed to execute* subsequent tasks.) Tasks are guaranteed to execute* sequentially, and no more than one task will be active at any* given time. Unlike the otherwise equivalent* {@code newFixedThreadPool(1)} the returned executor is* guaranteed not to be reconfigurable to use additional threads.** @return the newly created single-threaded Executor*/public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,4000new LinkedBlockingQueue<Runnable>()));}12345678910111213141516171819大致意思是:创建一个只有一个线程的线程池来操作不限数量的队列,也就是把线程放进了一个队列中,队列我们都知道是FIFO的。SingleThreadExecutor的工作线程只有一个,其他队列中的线程都处于休眠,也就是sleep状态,当这个worker线程做完事了,也就是run方法运行结束,就又从队列中拿出一个休眠线程(sleep)出来唤醒(notify),这样依次把队列中的所有线程处理完毕,这样并没有结束,如果线程池中没有待处理的线程,线程池一直会等待,等待下一次任务的提交,除非把线程池给shutdown掉,这样线程池的生命周期才算完毕。
相关文章推荐
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 三个线程依次顺序执行
- 三个线程按照顺序依次执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 三个线程依次顺序执行
- condition实现三个线程顺序执行《2》
- 三个线程轮流执行顺序打印ABC(三):使用Lock实现
- 现有T1、T2、T3三个线程,你怎样保证T2在T1执行完成之后执行,T3在T2执行完后执行?
- 如何保证线程执行的顺序
- 有三个线程,怎么让他们按顺序执行
- 有三个线程T1,T2,T3,下面方法可以确保它们按顺序执行的有()该线程继续执行
- C#中添加三个线程同时启动执行某一方法,并依次调用某方法中的循环打印输。
- 现有三个线程T1,T2,T3 三个线程依次执行
- CountDownLatch--控制三个线程执行顺序(三)
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
- C#中添加三个线程同时启动执行某一方法,并依次调用某方法中的循环打印输。
- 现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行
- Java 多线程 (PART VII) 使用join()方法保证线程的执行顺序