Home > other >  How to solve the thread lock after an exception occurs, perform sys. The exit () code lead to termin
How to solve the thread lock after an exception occurs, perform sys. The exit () code lead to termin

Time:10-15

I want to reach the purpose of: there are multiple be locked thread is running, if one thread exception happens, let program exits, echo other threads run no longer as a result,

But I found that if the program directly, the lock can not be released, the terminal will be stuck, I wrote a sample is as follows:



If first release to exit the program again, this time the program can be normal, but the content of the other threads will be displayed



Google a day also didn't find a way, how can I modify to achieve: a thread directly exit exception occurs, and other threads is not implemented? Hope everybody who walked past can give younger brother a prompt answer, crab crab

CodePudding user response:

I understand is executive, test1 test2 is executed, the output is according to the queue,

Can have a try if test2 is long enough, look to whether be interrupted,

Place of action test2
For I in range (100) :
Print (' test do ', I)
Sleep (1)


See if it is interrupted,



CodePudding user response:

reference 1st floor seakingx response:
I understand is executive, test1 test2 is executed, the output is according to the queue,

Can have a try if test2 is long enough, look to whether be interrupted,

Place of action test2
For I in range (100) :
Print (' test do ', I)
Sleep (1)


See if it is interrupted,
I write here is the sample code, if the test2 execution time is take temporary solution not effect a permanent cure

CodePudding user response:

Thread is the concept of parallel,
Such as thread 1 it takes 5 seconds
Thread 2 to 10 seconds, 3 seconds and 8 seconds have output
Thread 1, 2, and at the same time start
Thread 1 to 4 seconds when the exception, you can't ensure the output thread 2 3 seconds when the output,


1, to confirm whether normal interrupt test2
2, if you just imagine control output, can consider to using messaging, output before, first to determine whether the main thread has passed the interrupt signal,

CodePudding user response:

reference 4 floor seakingx response:
thread is the concept of parallel,
Such as thread 1 it takes 5 seconds
Thread 2 to 10 seconds, 3 seconds and 8 seconds have output
Thread 1, 2, and at the same time start
Thread 1 to 4 seconds when the exception, you can't ensure the output thread 2 3 seconds when the output,


1, to confirm whether normal interrupt test2
2, if you just imagine control output, can consider to use message mechanism, output before, first to determine whether the main thread has already passed the interrupt signal,
I to learn to understand the mechanism of information, thank you bosses
  • Related