Before we talked about thread creation, that was the first step in the thread lifecycle, followed by starting thread with the start () method, which performs some internal administrative work and then invokes the thread's run () method, where the thread is Alive (active), And we can also use the IsAlive () method to determine whether the thread is started or terminated.
Once thread is started, we can only execute one method: Run (), and the Run () method is responsible for performing the thread task, so the way to end thread is simply to terminate the run () method. Looking at the document carefully, we'll see that there is a way: Stop (), which seems to stop thread, but this method has been abolished because it has internal competition.
We often need a thread that executes continuously and then ends it under a certain condition, but there are many ways to do it, but the most common are two ways to set tags and interrupt thread.
We'll rewrite the thread in the previous example:
public class Randomcharactergenerator extends Thread implements Charactersource {static char[] chars;
static String Chararray = "abcdefghijklmnopqrstuvwxyz0123456789";
static {chars = Chararray.tochararray ();
Private volatile Boolean done = false;
Random Random;
Charactereventhandler handler;
Public Randomcharactergenerator () {random = new random ();
Handler = new Charactereventhandler ();
public int Getpausetime () {return (int) (Math.max (1000, 5000 * random.nextdouble ())); @Override public void Addcharacterlistener (Characterlistener cl) {Handler.addcharacterlistener (CL)
; @Override public void Removecharacterlistener (Characterlistener cl) {Handler.removecharacterlisten
ER (CL); @Override public void Nextcharacter () {handler.firenewcharacter (this, int) chars[ Random.nextint (chars.length)]);
public void Run () {while (!done) {nextcharacter ();
try {thread.sleep (Getpausetime ());
catch (Interruptedexception IE) {return;
}} public void Setdone () {done = true; }
}
Now we have one more tag: done so that we can decide when to stop the thread by calling Setdone () in the code. The volatile keyword is used here, mainly for synchronization. This will be put in sync here.
The biggest problem with setting tags is that we have to wait for the status of the tag, which can cause delays. Of course, this delay is unavoidable, but it has to be minimized. As a result, this method of interrupting thread has its place of play.
We can break thread through the interrupt () method, which causes two side effects:
1. It will cause any blocking method to throw interruptedexception, we must catch this error mandatory even if we do not need to deal with it, this is the Java exception handling mechanism is a place to be criticized.
2. Set the tag inside the thread object to indicate that this thread has been interrupted, like this:
public void Run
() {While
(!isinterrupted ()) {
...}}
}
Although delays cannot be avoided, delays have been shortened.
Whether marked or interrupted, the reason why we can not eliminate the delay is that we cannot determine whether the check mark first or the method first, this is the so-called race condition, is the thread processing can never avoid the topic.
Thread can not only be terminated, it can be paused, suspended, and restored.
Thread originally had the suspend () method and the Resume () method to perform the suspend and restore, but they and the stop () were abolished for the same reason.
We can suspend thread through the sleep () method, and it will recover automatically after the specified time. In a strict sense, sleep is not the same as suspend, the real suspend should be one thread to suspend another, but sleep will only affect the current thread. To really implement hangs and restores, we can use the wait and notify mechanism, but the biggest problem with this mechanism is that our thread must be written with this technique.
Thread after the end, if possible, we still need to clean it. Even if thread has been terminated, other objects can invoke the thread's resources as long as they hold its reference, which could also cause the thread to not be reclaimed.
But we sometimes want to keep this thread's reference, because we want to determine whether it really has finished working, and we can use the join () method. The join () method is blocked until thread completes its run () method, but there is a risk that the first call to the join () method may remain blocked for a long time until thread is really finished, so we typically use the IsAlive () method To judge.
Since we can define our task by implementing a runnable interface, there is a problem in determining if the thread has been interrupted: The task is not yet bound to any thread. We can use the CurrentThread () method to get a reference to the current thread, and then call isinterrupted () to determine if the thread is interrupted.