The multi-thread test sub-thread of Junit unit is not executed, and the multi-thread test of junit is

Source: Internet
Author: User

The multi-thread test sub-thread of Junit unit is not executed, and the multi-thread test of junit is
The child thread of the Junit unit test is not executed.

The child thread of the Junit unit test is not executed.

Environment
-Junit: 4
-Jdk: 1.8

1. symptom description

When you test the CountDownLatch class, you can use the main function to run normally. When you use the Junit function for testing, debugging does not go to the sub-thread for execution. When you run it directly, sometimes some code in the subthread can be executed. The specific program is as follows:

Import org. junit. test; import java. util. concurrent. countDownLatch;/*** @ ClassName: CountDownLatchTest * @ Description: countDownLatch Test class * @ author Yue Chang * @ date January 16, 2018 11:31:26 * @ since 1.0 */public class CountDownLatchTest {@ test public void Test () throws InterruptedException {CountDownLatch latch = new CountDownLatch (1); MyThread myThread = new MyThread (latch); Thread t1 = new Thread (myThread); t1.setName ("t1 "); thread t2 = new Thread (myThread); t2.setName ("t2"); t1.start (); t2.start ();} public static void main (String [] args) {CountDownLatch latch = new CountDownLatch (1); MyThread myThread = new MyThread (latch); Thread t1 = new Thread (myThread); t1.setName ("t1 "); thread t2 = new Thread (myThread); t2.setName ("t2"); t1.start (); t2.start () ;}} class MyThread implements Runnable {private CountDownLatch latch; private volatile int number; /*** @ param latch */public MyThread (CountDownLatch latch) {this. latch = latch;} public MyThread () {} public CountDownLatch getLatch () {return latch;} public void setLatch (CountDownLatch latch) {this. latch = latch;} public int getNumber () {return number;} public void setNumber (int number) {this. number = number;} public void run () {String threadName = Thread. currentThread (). getName (). trim (); if ("t2 ". equals (threadName) {// t2 Thread try {Thread. sleep (1, 5000); System. out. println ("Thread 0:" + threadName);} catch (InterruptedException e) {e. printStackTrace ();} this. setNumber (1); latch. countDown (); // count minus 1} else if ("t1 ". equals (threadName) {// t1 thread try {System. out. println ("thread 1:" + threadName); latch. await (); // The blocking wait count is 0 System. out. println ("thread 1:" + threadName);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println ("num =" + this. getNumber ());}}}
2. Execution result

The result of Junit execution is as follows:

Thread 1: t1

Or nothing is printed.

The main execution result is as follows:

Thread 1: t1 Thread 0: t2 thread 1: t1num = 1

When t1 is executed, because the countDownLatch value is not 0, it will be in the waiting state. If the execution of t2 is completed, the countDownLatch value will be reduced by 1 and changed to 0, t1 will continue to be executed, finally, print the num.

3. Analysis

The following information shows that when the main thread exits, the sub-thread also exits. In this way, we can explain why the execution part is not executed at all.

4. solution:

Add the following code to the Junit test code:

// After the main junit thread is executed, the execution of the pipe thread will not be performed. If there is no join (), the subthread cannot execute t1.join () in most cases ();

Let the main thread wait for t1 thread to finish execution, so that the sub-thread can be executed normally.

View comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.