如何等待一个线程产生它自己的线程

如何等待一个线程产生它自己的线程

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

问题描述

我正在尝试测试一种在单独线程中工作的方法,简化如下:

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.

我怎样才能让第一个线程等待它启动的线程以及该线程启动的任何线程?

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?

推荐答案

如果不引用在 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-like object 而不是线程,如果你想扩展 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.

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

08-05 22:24