• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JUnit测试未执行测试创建的所有线程

用户头像
it1352
帮助3

问题说明

我写了一个简单的线程示例,它为从1到20的数字生成表.当我使用main方法对其进行测试时,它会执行所有线程(打印所有消息),而不会运行所有线程(所有消息)与JUnit测试相同时,大多数时候(有时它会运行所有线程)都不会被打印.我认为在产出方面应该没有任何区别.

I wrote a simple example for threading which generates tables for numbers starting from 1 to 20. when I tested it with main method it executes all the threads (prints all the messages), while all threads are not being run (all messages are not being printed) most of the times (sometimes it runs all the threads) when doing the same with JUnit test. I think there should not be any difference in terms of output.

这是具有主要方法的类:

Here is the class with main method:

public class Calculator implements Runnable {

    private int number;

    Calculator(final int number){
        this.number = number;
    }

   @Override
   public void run() {
        for(int i = 1; i <= 10; i  ){
            System.out.printf("%s : %d * %d =  %d \n", Thread.currentThread().getName(), number, i, number * i);
        }

   }

    public static void main(String[] args){
        Calculator calculator = null;
        Thread thread = null;
        for(int i = 1; i < 21; i   ){
            calculator = new Calculator(i);
            thread = new Thread(calculator);
            System.out.println(thread.getName()   " Created");
            thread.start();
            System.out.println(thread.getName()   " Started");
        }

     }

}

当我调用main方法时,它会打印所有结果.

When I invoke the main method it prints all the results.

下面是与主要方法等效的JUnit测试代码:

Bellow is the code for JUnit test equivalent to the main method:

public class CalculatorTest {

private Calculator calculator;
private Thread thread;

@Test
public void testCalculator() {
    for(int i = 1; i < 21; i   ){
        calculator = new Calculator(i);
        thread = new Thread(calculator);
        System.out.println(thread.getName()   " Created");
        thread.start();
        System.out.println(thread.getName()   " Started");
     }
 }

}

当我运行上述测试用例时,输出的行为在场景中不一致,有时它会打印所有消息,并且大多数情况下仅打印少数几个并退出.这是在上述JUnit测试用例的情况下捕获的输出:

When I run the above test case, the behavior of the output is not consistant in the scene that sometimes it prints all the messages and most of the times prints only a few and exits. Here is the output captured in case of the above JUnit test case:

Thread-0 Created
Thread-0 Started
Thread-1 Created
Thread-1 Started
Thread-2 Created
Thread-2 Started
Thread-3 Created
Thread-3 Started
Thread-4 Created
Thread-4 Started
Thread-5 Created
Thread-5 Started 
Thread-6 Created
Thread-6 Started
Thread-7 Created
Thread-7 Started
Thread-8 Created
Thread-8 Started
Thread-9 Created
Thread-9 Started
Thread-10 Created
Thread-10 Started
Thread-11 Created
Thread-11 Started
Thread-12 Created
Thread-12 Started
Thread-13 Created
Thread-13 Started
Thread-14 Created
Thread-14 Started
Thread-15 Created
Thread-15 Started
Thread-16 Created
Thread-16 Started
Thread-17 Created
Thread-17 Started
Thread-18 Created  
Thread-18 Started
Thread-19 Created 
Thread-19 Started
Thread-0 : 1 * 1 =  1 
Thread-0 : 1 * 2 =  2 
Thread-0 : 1 * 3 =  3 
Thread-0 : 1 * 4 =  4 
Thread-0 : 1 * 5 =  5 
Thread-0 : 1 * 6 =  6 
Thread-0 : 1 * 7 =  7 
Thread-0 : 1 * 8 =  8 
Thread-0 : 1 * 9 =  9 
Thread-0 : 1 * 10 =  10 
Thread-2 : 3 * 1 =  3 
Thread-2 : 3 * 2 =  6 
Thread-2 : 3 * 3 =  9 
Thread-2 : 3 * 4 =  12 
Thread-2 : 3 * 5 =  15 
Thread-2 : 3 * 6 =  18 
Thread-2 : 3 * 7 =  21 

输出到此结束,而不会在其他线程中打印其余消息/也不在执行其他线程.

Output ends here without printing the remaining messages in other threads/ executing other threads.

有人可以帮助我了解其背后的原因吗?预先感谢.

Can somebody help me to understand the reason behind this. Thanks in advance.

正确答案

#1

JUnit正在提早退出测试方法.您需要等待所有线程完成才能退出testCalculator()方法.

JUnit is exiting the test method early. You need to wait for all of the threads to complete before you exit the testCalculator() method.

一种简单的方法是使用CountDownLatch.

An easy way to do that is by using a CountDownLatch.

  1. CountDownLatch latch = new CountDownLatch(20)初始化CountDownLatch.

将每个可运行的Calculator引用传递给锁存器.在run()方法的末尾,调用latch.countDown().

Pass each Calculator runnable a reference to the latch. At the end of the run() method, call latch.countDown().

testCalculator()方法的末尾调用latch.await().这将一直阻塞,直到latch.countDown()被调用20次(即所有线程都已完成)为止.

At the end of the testCalculator() method call latch.await(). This will block until latch.countDown() has been called 20 times (i.e. when all threads have completed).

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcakehi
系列文章
更多 icon
同类精品
更多 icon
继续加载