顺序运行Java线程 [英] Run Java Threads sequentially

查看:99
本文介绍了顺序运行Java线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何顺序执行三个线程?例如。 Thread1,Thread2,Thread3。不可能将一个线程的引用传递给另一个线程,并从run()方法调用。

How will you execute Three threads sequentially? For eg. Thread1, Thread2, Thread3. It is not possible to pass the reference of one Thread to the other and invoke from the run() method.

所以代码应该是这样:

 Thread1.start();
 Thread2.start();
 Thread3.start();

并且输出应为

 Printing Thread1
 Printing Thread2
 Printing Thread3

推荐答案

使用可以通过使用ThreadPoolExecutor和使用阻塞队列, href =http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html> ExecutorService 在java.util.concurrent包中。更确切地说,使用 Executors.newSingleThreadExecutor();

Use ExecutorService in java.util.concurrent package. More precisely use Executors.newSingleThreadExecutor();

这篇关于顺序运行Java线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆