如何等待一个产生它自己的线程的线程? [英] How to wait for a thread that spawns it's own thread?

查看:108
本文介绍了如何等待一个产生它自己的线程的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个在单独的线程中工作的方法,简化它是这样的:

I'm trying to test a method that does it's work in a separate thread, simplified it's like this:

public void methodToTest()
{
    Thread thread = new Thread()
    {
        @Override
        public void run() {
            Clazz.i = 2;
        }
    };
    thread.start();
}

在我的单元测试中,我想测试一下Clazz.i == 2,但我不能这样做,因为我认为断言是在线程更改值之前运行的。我想过使用另一个线程来测试它,然后使用join来等待,但它仍然无效。

In my unit test I want to test that Clazz.i == 2, but I can't do this because I think that the assert is run before the thread changes the value. I thought of using another thread to test it and then use join to wait but it still doesn't work.

SSCCE:

@Test
public void sscce() throws InterruptedException
{
    Thread thread = new Thread()
    {
        @Override
        public void run() {
            methodToTest()
        }
    };
    thread.start();     
    thread.join();  
    AssertEquals(2, Clazz.i);
}

public static class Clazz
{
    public static int i = 0;
}

我认为这是因为测试主代码创​​建了一个等待的线程(加入)到第二个线程,但第二个线程不做工作,它创建另一个线程来完成工作然后完成,继续第一个线程,而第三个线程执行 Clazz。断言后i = 2

I think this is because the test main code creates a thread that is waiting (joined) to the 2nd thread, but the 2nd thread doesn't do the work, it creates another thread to do the work and then finishes, which continues the first thread, while the third thread does the Clazz.i = 2 after the assertion.

如何使第一个线程等待它开始的线程 as以及该线程启动的任何线程

How can I make it so that the first thread waits for the thread that it starts as well as any threads that that thread starts?

推荐答案

没有对<$ c $中创建的线程的引用c> methodToTest ,你不能,非常简单。 Java没有提供查找在这个特定时间段内生成的线程的机制(即使它确实存在,也可能是一个丑陋的机制)。

Without a reference to the thread created in methodToTest, you cannot, quite simply. Java provides no mechanism for finding "threads that were spawned during this particular time period" (and even if it did, it would arguably be an ugly mechanism to use).

在我看来,你有两个选择:

As I see it, you have two choices:


  • 制作 methodToTest 等待线程它产生。当然,如果您明确希望这是一个异步操作,那么您就不能这样做。

  • methodToTest返回新创建的线程,以便任何来电者可以选择等待,如果他们愿意的话。

  • Make methodToTest wait for the thread it spawns. Of course, if you explicitly want this to be an asynchronous action, then you can't very well do that.
  • Return the newly created thread from methodToTest, so that any callers can choose to wait for it if they so wish.

可能会注意到第二种选择可以用几种不同的方式制定。例如,您可以返回一些抽象的 Future 类对象而不是线程,如果你想扩展 methodToTest 的自由,可以使用各种方式进行异步工作。您也可以定义一些全局任务池,强制执行所有异步任务以在其中运行,然后在检查断言之前等待池中的所有任务完成。这样的任务池可以采用 的形式ExecutorService ,或 ThreadGroup 或任意数量的其他表单。它们最终都做同样的事情,但可能或多或少地适合你的环境 - 主要的一点是你必须显式地给调用者访问新创建的线程,是一些方式或其他方式。

It may be noted that the second choice can be formulated in a few different ways. You could, for instance, return some abstract Future-like object rather than a thread, if you want to extend the liberty of methodToTest to use various ways of doing asynchronous work. You could perhaps also define some global task-pool that you enforce all your asynchronous tasks to run inside, and then wait for all tasks in the pool to finish before checking the assertion. Such a task pool could take the form of an ExecutorService, or a ThreadGroup, or any number of other forms. They all do the same thing in the end, but may be more or less suited to your environment -- the main point being that you have to explicitly give the caller access to the newly created thread, is some manner or another.

这篇关于如何等待一个产生它自己的线程的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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