I just started to learn multithreading today. Although I have used it a little before, I just copied the code on the Internet and didn't really understand it. Now I want to study it and understand it slowly, the following is an example of the information I found online. I believe you have read it and made some minor changes.
Using system; using system. threading; namespace BIOVision. iHospital. his. basic {public class simple {public static int main () {console. writeline ("thread start/stop/join sample"); Alpha alpha = new alpha (); thread t = new thread (New threadstart (alpha. beta); T. start (); While (! T. isalive) thread. sleep (1); T. abort (); // T. join (); console. writeline ("Alpha. beta has ended and the execution status is "+ T. isalive. tostring () + "thread status:" + T. threadstate. tostring (); try {console. writeline ("trying to restart Alpha. beta "); T. start ();} catch (threadstateexception) {console. writeline ("threadstateexception trying to restart t thread"); console. writeline ("t threads cannot be restarted after termination"); console. readline () ;}return 0 ;}} public Class Alpha {public void beta () {While (true) {console. writeline ("Alpha. beta is running ");}}}}
This code is very understandable, but what I don't understand is that I used to think that the abort () method will terminate the thread. Why do I need to call the join method of thread t? The official explanation is abort (): to start the process of terminating this thread. Calling this method usually terminates this thread. Join (): stops calling a thread, until a thread is terminated. At that time, I couldn't get through these two sentences until I put t. isalive (indicating the execution status of the current thread) and T. threadstate (this value contains the state of the thread) is printed and then want to understand, now first look at the running results of the above Code:
Strange: Why is the isalive execution status still true after the thread abort ()? Let's look at the thread status as abortrequested. This means that the thread has called abort, but the thread has not stopped yet, so think about the reason that the thread has not been stopped and the isalive attribute is true. Then, restarting the thread in the try statement block will produce an error. Why?
But the problem has not been solved. How can we terminate the thread? Cannot abort terminate the purebred? Well, don't think so much. Let's remove the T. Join () annotator in the above Code and run the program. The result is as follows:
Okay, now let's look at the execution off state and thread state. We know that the thread is terminated, and we know that when a thread calls abort (), it cannot start () any more, therefore, an exception is thrown and a catch statement is executed. In both cases, I thought at first that it was impossible to terminate the thread by using only abort () without join? However, it is not difficult to come to the following conclusion after a detailed introduction of the two results and the two methods:
1. the abort () function is used to terminate the thread that calls this method, but in most cases it takes a little time, some latency (this thread may still be executed in a short time )...
2. the function of the join () method is not to terminate the thread, but to stop the T thread execution that is being terminated (the abort () method has been called but has not been terminated) before the T thread ends, at the same time, wait for the main thread until the T thread ends (that is, the abort () method ends) and then execute the following code. The execution status of the printed result is false, the thread status is stopped.
Note:: Before the join method is called, the execution result of this Code may be two results in the figure, which may be related to the execution time of abort () according to different computers, here, the join ensures that the T thread terminates the code below. I think this is the magic behind the use of join after abort!