访谈:如何确保一个接一个的线程运行? [英] Interview: How to ensure that a thread runs after another?

查看:88
本文介绍了访谈:如何确保一个接一个的线程运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有线程 T1 T2 T3 ,我们如何确保线程 T2 T1 之后运行并且线程 T3 T2之后运行

There are thread T1, T2 and T3, how can we ensure that thread T2 run after T1 and thread T3 run after T2?

在我的采访中询问了这个问题。我没回答。请详细解释。

This question was asked in my interview. I didn't answer. Please explain in detail.

推荐答案

这将是最简单,最愚蠢的方法:

This would be the simplest, dumbest approach:

final Thread t1 = new Thread(new T1()); // assume T1 is a Runnable
t1.start();
t1.join();
final Thread t2 = new Thread(new T2());
t2.start();
t2.join();
final Thread t3 = new Thread(new T3());
t3.start();
t3.join();

这篇关于访谈:如何确保一个接一个的线程运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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