线程按顺序运行 [英] threads running in sequence

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

问题描述

我是Java的新手,我想知道如何做一个在面试问题中被问到的事情.

I am new to java and I am wondering how to do something I was asked in an interview question.

您有三个线程T1T2T3.
如何确保它们按T1T2T3的顺序完成.
面试官让我在上面写程序.
我知道可以用joincount 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

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

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