问题描述
我的任务是按此顺序创建线程:如果A开始 - >开始B和C,如果B开始 - >开始D.
并按相反顺序销毁它们如果D然后B.如果B和C然后A.我希望你明白。我设法做到了,但我想有更好的方法来做到这一点。你有什么建议吗?
my task is to create thread in this order: if A start->start B and C, if B start->start D.And destroy them in reverse order If D then B. If B and C then A. I hope you get it. I manage to do it but I guess there is better way to do it. Do you have any suggestions?
在您发表评论之后,我更改了我的代码,而且更简单。但现在它看起来愚蠢。我想改变if语句和实现的核心,有什么建议吗? tnx的建议我正在和你学习。
After your comments i have changed my code and it is much more simply. But now it looks "stupid". I would like to change hardcore of if statements and implementation, any advice? tnx for advice I'm learning with you.
这是我的新代码:
import java.util.*;
class RobotController implements Runnable{
String name;
public void run() {
Thread t = Thread.currentThread();
System.out.println(t.getName() + " status = " + t.isAlive());
System.out.println(t.getName() + " status = " + t.getState());
}
public static void main(String args[]) throws InterruptedException{
Thread thread_A = new Thread(new RobotController(), "Thread A");
Thread thread_B = new Thread(new RobotController(), "Thread B");
Thread thread_C = new Thread(new RobotController(), "Thread C");
Thread thread_D = new Thread(new RobotController(), "Thread D");
thread_A.start();
thread_A.join();
System.out.println(thread_A.getState());
thread_B.start();
thread_B.join();
System.out.println(thread_B.getState());
thread_C.start();
thread_C.join();
System.out.println(thread_C.getState());
thread_D.start();
System.out.println(thread_D.getState());
}
}
推荐答案
您的代码中存在一些缺陷,有时会导致其无法正常工作:
There are some flaws in your code which will make it not to work accordingly sometimes:
- 你调用
thread_A.start()
,然后检查thread_A.isAlive()
。现在如果,在检查thread_A.isAlive()
条件之前,thread_A已经完成怎么办?thread_B
和thread_C
永远不会启动。您的申请失败。 - 假设
thread_A
未完成且thread_A.isAlive() thread_B
之前启动条件,而Java线程调度程序并不总是保证thread_C
。您的应用程序再次失败。 - 假设
thread_B
在thread_C
之前启动,如果thread_B
在thread_B.isAlive()
被检查之前完成,然后如果
条件失败,thread_D
永远不会启动。您的申请再次失败。
- You called
thread_A.start()
and then checkedthread_A.isAlive()
. Now what if , thread_A is already completed beforethread_A.isAlive()
condition is checked?.thread_B
andthread_C
is never started. Your application fails. - Assume that
thread_A
is not completed andthread_A.isAlive()
condition is passed, then starting ofthread_B
beforethread_C
is not always guaranteed by Java thread scheduler. Again your application fails. - Assume that
thread_B
starts beforethread_C
and ifthread_B
completes beforethread_B.isAlive()
is checked then theif
condition fails andthread_D
is never started. Again your application fails.
现在需要思考:
无需检查如果在调用 join()
方法后线程处于活动状态。这是一个不必要的运行时开销。
Now a point to ponder:
There is no need to check if the thread is alive after its join()
method is called. It is an unnecessary runtime overhead.
编辑
好的,这是修改过的代码版本。我希望它能让你理解线程的动态:
EDIT
OK, Here is the modified version of code..I hope it would let you understand the dynamics of thread:
class RobotController implements Runnable
{
private final Object lock = new Object();
private void notifyThread()
{
synchronized(lock)
{
lock.notify();
}
}
public void run()
{
synchronized(lock)
{
try
{
System.out.println(Thread.currentThread().getName() + " started");
lock.wait();
System.out.println(Thread.currentThread().getName()+ " stopped");
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String args[]) throws InterruptedException
{
RobotController rca = new RobotController();
RobotController rcb = new RobotController();
RobotController rcc = new RobotController();
RobotController rcd = new RobotController();
Thread thread_A = new Thread(rca,"Thread A");
Thread thread_B = new Thread(rcb,"Thread B");
Thread thread_C = new Thread(rcc,"Thread C");
Thread thread_D = new Thread(rcd,"Thread D");
thread_A.start();
while (thread_A.getState() != Thread.State.WAITING)
{
Thread.sleep(100);
}
thread_B.start();
thread_C.start();
while (thread_B.getState() != Thread.State.WAITING && thread_C.getState() != Thread.State.WAITING)
{
Thread.sleep(100);
}
thread_D.start();
while (thread_D.getState() != Thread.State.WAITING)
{
Thread.sleep(100);
}
rcd.notifyThread();
thread_D.join();
rcc.notifyThread();
thread_C.join();
rcb.notifyThread();
thread_B.join();
rca.notifyThread();
}
}
这是输出:
Thread A started
Thread B started
Thread C started
Thread D started
Thread D stopped
Thread C stopped
Thread B stopped
Thread A stopped
这篇关于同步线程执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!