ABC线程如何保证顺序执行 关于多线程的一个小例子
2018-03-07 18:02
344 查看
(谨以此贴感谢boss直聘首席架构师李川的指导)
这个例子是某架构师让我回来一定要理解体会试着做的一个例子,主要是避免对线程的纸上谈兵:
先来段源码
public class AbcThread {
public static void main(String[] args) {
final Thread t1 =new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 1");
}
},"T1");
final Thread t2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 1");
try{
t1.join(10);
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"T2");
final Thread t3=new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 3");
try{
t2.join(10);
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"T3");
ExecutorService executorService= Executors.newSingleThreadExecutor();
executorService.submit(t1);
executorService.submit(t2);
executorService.submit(t3);
executorService.shutdown();
}
}
这个例子是某架构师让我回来一定要理解体会试着做的一个例子,主要是避免对线程的纸上谈兵:
先来段源码
/** * 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, new LinkedBlockingQueue<Runnable>())); }
public class AbcThread {
public static void main(String[] args) {
final Thread t1 =new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 1");
}
},"T1");
final Thread t2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 1");
try{
t1.join(10);
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"T2");
final Thread t3=new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"run 3");
try{
t2.join(10);
}catch (InterruptedException e){
e.printStackTrace();
}
}
},"T3");
ExecutorService executorService= Executors.newSingleThreadExecutor();
executorService.submit(t1);
executorService.submit(t2);
executorService.submit(t3);
executorService.shutdown();
}
}
相关文章推荐
- JDK中的Timer和TimerTask详解 目录结构: Timer和TimerTask 一个Timer调度的例子 如何终止Timer线程 关于cancle方式终止线程 反复执行一个任务 sche
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 多线程中如何保证某段程序 连续执行,不被其他线程干扰
- 如何保证线程执行的顺序
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行
- Java 多线程 (PART VII) 使用join()方法保证线程的执行顺序
- C# 如何让 多线程中每个线程间隔毫秒执行同一个方法
- 如何保证线程按顺序执行
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
- volatile变量在多线程同步时运用的优点: 1、锁一次只允许一个线程访问值,volatile 允许多个线程执行读操作,因此当使用 volatile 保证读代码路径时,要比使用锁执行全部代码路径获得
- java中给出一个子线程如何捕获主线程异常的例子
- 如何控制线程的执行顺序
- 如何控制线程执行的顺序?
- Java多线程系列四——控制线程执行顺序
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- Linux多线程编程时如何查看一个进程中的某个线程是否存活
- 在 Java 的多线程中,如何去判断给定的一个类是否是线程安全的(另外:synchronized 同步是否就一定能保证该类是线程安全的。)
- TestNG入门教程(TestNG介绍、在Eclipse中安装TESTNG、测试小例子、基本注解、如何执行测试、按顺序执行Case、异常测试、组合测试、参数化测试、忽略测试、依赖测试、测试结果报告)
- 面试必问!Java 多线程中两个线程交替执行,一个输出偶数,一个输出奇数