Java multithreading – how to properly terminate threads

Recently, I’d like to read the actual Java high concurrent programming, and consolidate the knowledge of Java multithreading. Next, you should write a series of book notes, which contain multiple references to the code or text in the book. This is the first article.

The stop method is not recommended

Thread.stop () is an obsolete method that is not recommended because the stop method is too violent, forcing the execution of half of the thread to terminate, and immediately releasing all the locks of this thread. Destroys the consistency of the referenced objects in the thread.

For example, a user table is maintained in the database, which records user ID and user name, and writes two records using Thread multithreading:

Records 1:, ID=1, NAME=, Xiao Ming records, 2:, ID=2, NAME=, Wang

If stop ends when the record 1 is written in half, there may be various strange phenomena:

  1. Record 1 was recorded, 2 covered, with no data left.
  2. Record 1 is only half, that is, only ID, and NAME is empty.
  3. Record 1 and record 2 are mixed in the same record, and finally write a half, record 1, half is record 2 dirty data

So, unless you’re pretty sure what you’re doing, don’t use the stop method easily

Use the method of judging flag bits to interrupt threads

Well, what do we need to do if there is a need to interrupt the thread? In general, we immediately think of the method of setting flag bits, that is, before each step of the thread is executed, it is determined whether or not the thread needs to be exited:

Class WorkThread extends Thread private Boolean {mExitThread = false; public void (exitThread) {mExitThread = true;} @Override public void (run) {if (mExitThread) {return}; //do something if (mExitThread) {return}; //do something if (mExitThread) {return}}}; //do something

In fact, the Thread class has helped us to implement this interruption mark. There are three methods associated with Thread interrupts:

Public void (Thread.interrupt)
public native Boolean / / thread interrupts (Thread.isInterrupted) to determine whether it is
public / / static native Boolean Thread.interrupted interrupt (/ /) to determine whether the interrupt, interrupt and clears the current state

So the code above can be rewritten like this:

Class WorkThread extends Thread @Override public void (run) {{if ((isInterrupted)) {return}; //do something if (isInterrupted) (//do something) {return}; if ((isInterrupted)) {return}}}; //do something

At this point, we simply need to call the Thread.interrupt () method to break the thread safely.

Need to remind is (Thread.interrupt) method and Thread.stop method) (not as immediately as the end of the thread, it just set an interrupt flag, need to manually determine the sign in code implementation and launch.

Like the following code, even dropping the Thread.interrupt () method does not break threads:

Class, WorkThread, extends, Thread, {@Override, public, void, run (), {while (true), {//do, something}}

Advantages of Thread.interrupt

Using Thread.interrupt to interrupt threads can also interrupt threads in sleep and wait in addition to eliminating the worry of implementing flag bits themselves.

Remember when we call Thread.sleep () this method requires catch or throws the InterruptedException exception?:

Try {Thread.sleep (1000);} catch (InterruptedException, e) {e.printStackTrace ()}

This InterruptedException exception is thrown because we called the Thread.interrupt method. So Thread.interrupt can interrupt Thread.sleep. Similarly, Thread.wait can also be interrupted by Thread.interrupt.

It should be noted that if the sleep method throws an exception because of an interruption, it clears the interrupt flag. So you need to set the interrupt flag again when catch arrives at this exception:

Thread T1 = new (Thread) @Override public void (run) {{{while (true) if (Thread.currentThread) (.IsInterrupted () {break}); System.out.println ("Hello world"); try{Thread.sleep (1000);}catch (InterruptedException E) {Thread.currentThread (.Interrupt));}}}} (;