简体   繁体   中英

Why are my threads NOT exiting when the Test Suite finishes?

I have created an automated test suite which has a thread pool running simultaneously in the background of all test cases in order to obtain given system and performance metrics. Each thread is using a JSch connection to execute its shell commands and they are receiving [JSchException: Channel not opened exceptions].

The key problem is that the test suite continues to run forever, because the threads are not exiting even when all test cases have ended. But I'm not sure why...

When I checked the thread dump, I found that the threads do not exit because they are in a BLOCKED status.

Does anybody have an explanation for this? Or some help in resolving this issue?

I guess you mean the shutdownNow() method of ExecutorService. This is the documentation (I formatted the relevant parts in bold):

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

This method does not wait for actively executing tasks to terminate . Use awaitTermination to do that.

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.

Thread interruption is a cooperative process, your threads must be implemented so that they respond to interruption.

EDIT

1) About Thread.currentThread.interrupt() see this question: Why invoke Thread.currentThread.interrupt() when catch any InterruptException?

So you need to do this only if you have other methods/thread groups watching the interrupted status. In your case, probably not, but it does not hurt.

2) It seems that in "ShellUtils.executeCommand" you are running external programs. This could be the source of the problem if this method does not react to thread interruptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM