问题描述
我是Java的新手,我想知道如何做一个在面试问题中被问到的事情.
I am new to java and I am wondering how to do something I was asked in an interview question.
您有三个线程T1
,T2
和T3
.
如何确保它们按T1
,T2
,T3
的顺序完成.
面试官让我在上面写程序.
我知道可以用join
或count down latch
的概念来回答这个问题,但是有些原因使我无法成功.你们能告诉我如何去做吗?
You have three threads T1
, T2
and T3
.
How do you ensure that they finish in order T1
, T2
, T3
.
Interviewer asked me to write the program on it.
I know that this question can be answered by the concept of join
or count down latch
but some how I was not able to be successful.Could you guys please show me how to go about doing this?
推荐答案
创建3个线程,但不要启动它们,然后执行以下操作
Create 3 threads but dont start them, then go as follows
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
Thread.join等待线程终止,因此可以保证顺序
Thread.join waits for a thread to terminate, so the order is guaranteed
这篇关于线程按顺序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!