多线程中:如何确定哪个线程先停止
编写一个名为 RaceHorse 的类,它扩展了 Thread。每匹 RaceHorse 都有一个名称和 run() 方法,该方法会显示该名称 5000 次。编写一个 Java 应用程序来实例化 2 个 RaceHorse 对象。最后跑完的赛马就是失败者。
这就是问题所在。 我已经为两个运行线程的两个类编写了代码 以下是代码:
RaceHorse
class RaceHorse extends Thread
{
public String name;
public RaceHorse(String name)
{
this.name = name;
}
public void run()
{
for(int i = 1 ; i <= 5000; i++)
{
System.out.println(i+" "+name);
}
System.out.println(name+" finished.");
}
}
Runner
class Runner{
public static void main(String args[])
{
RaceHorse obj = new RaceHorse("Lol");
RaceHorse obj2 = new RaceHorse("BOL");
Thread t = new Thread(obj);
Thread t2 = new Thread(obj2);
t.start();
t2.start();
}
}
现在我的问题是我无法找到哪个线程首先完成以及哪秒完成,即哪匹马获胜以及输了。!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先:您的 RaceHorse 对象本身就是线程。您应该能够说
obj.start();
并且它也能正常工作。因此,完全删除t
和t2
。接下来,您需要某种方法来通知主线程有关获胜者的信息。
First off: your
RaceHorse
objects are themselves threads. You should be able to sayobj.start();
and it'd work just as well. So removet
andt2
entirely.Next, you'll need some way to notify the main thread about the winner.
毫无疑问有更好的方法,但一种方法可能是创建一个线程安全的类(例如“Trophy”),有一个仅在第一次调用时返回 true 的方法“getTrohpy”,并传递对实例的引用两个线程都获得奖杯。
There's no doubt a better way, but one method might be to create a class (e.g. 'Trophy') that is thread safe, has a method 'getTrohpy' that only returns true on the first call, and pass a reference to an instance of Trophy to both threads.
正如 cHao 指出的,
RaceHorse
扩展了Thread
,但您为每匹马创建了一个新的Thread
。我会以相反的方式解决这个问题,让RaceHorse
实现Runnable
来代替。其次,使用同步方法的解决方案是可行的,但一般规则始终是在 java.util.concurrent 中查找能够首先解决问题的类。这个问题可以使用 AtomicReference 来解决,以确保只有一匹马获得奖杯。
最后,如果主线程以固定顺序启动马的线程(这取决于虚拟机以及在操作系统上启动新线程的开销),则可能会偏向于马#1。)考虑使用所有马匹在出发前等待的信号(例如
CountDownLatch
)。As cHao pointed out,
RaceHorse
extendsThread
but you are creating a newThread
per horse. I would solve it the opposite way, by havingRaceHorse
implementRunnable
instead.Secondly, the solution using a
synchronized
method will work, but a general rule is always look for a class injava.util.concurrent
that will solve the problem first. This one can be solved using anAtomicReference
to ensure that only one horse takes the trophy.Lastly, there could be a bias in favour of horse #1, if the main thread starts the horses' threads in a fixed order (this depends on the VM and on the overhead of starting a new thread on your OS.) Consider using a signal (for example a
CountDownLatch
) that all horses wait for before starting.我不会为你编写代码;但您应该查看
notify
方法(请参阅 此处)。一种方法可能是:一旦线程完成,它将
wait()
等待其他线程通知(或notifyAll()
)。另一种更优雅的解决方案是在共享对象上使用同步块;
syncrhonized(obj)
语句将位于 run() 方法的末尾。在该语句中,您可以放入打印行或您认为有助于确定谁赢得比赛的任何其他代码。I am not going to write the code for you; but you should take a look at the
notify
method (see here) to be used.One approach could be: once a thread has finished it will
wait()
for the other thread(s) to notify (ornotifyAll()
).Another, more elegant solution, would consist of using a
synchronized
block on a shared object; thesyncrhonized(obj)
statement would be at the end of the run() method. Into that statement you could put a printline or any other code you would deem useful to determine who won the race.这将在 main 的末尾起作用:
This will work at the end of the main :
我迟到了,但我在寻找如何处理多个正在运行的线程的第一个结果时发现了这一点。我认为最简单的方法是使用 ArrayBlockingQueue 它给你这样的东西。
I'm late to the party, but I found this while looking for how to process the first result from a number of running threads. I think the easiest way is to use an ArrayBlockingQueue which gives you something like this.