There is no secure preemptive method to stop threads in Java, there is only one collaborative mechanism.
It is broadly divided into two collaborative mechanisms: 1. Set a flag that has been canceled, and the thread task periodically looks at the flag. End Task If the cancellation flag is set to True
2. The interrupt () of the calling thread can interrupt the target thread and query through the Thread.CurrentThread (). isinterrupted () method. You can also throw a Interruptedexception exception by using most of the blocking library functions, such as Thread.Sleep and object.wait, to exit the thread in an exception.
Implementation code for the first interrupt mode:
public class Primegenerator implements runnable{
Private final list<biginteger> primes=new arraylist<biginteger> ();
Private volatile Boolean cancelled; You need to set cancelled to a volatile variable to ensure that when one thread is modified, it is visible to other threads
@Override
public void Run () {
TODO auto-generated Method Stub
BigInteger P=biginteger.one;
while (!cancelled) {
P=p.nextprobableprime ();
Synchronized (this) {
Primes.add (P);
}
}
}
public void Cancel () {
Cancelled=true;
}
}
This interrupt mode causes the thread to block when the task dispatches a blocking method, such as the Blockingqueue.put method, and the task may never check the cancellation flag, so a cancellation failure occurs
Implementation code for the second interrupt mode:
public class Primeproducer implements runnable{
Private final blockingqueue<biginteger> queue;
Primeproducer (blockingqueue<biginteger> queue) {
This.queue=queue;
}
public void Run () {
TODO auto-generated Method Stub
try {
BigInteger p = biginteger.one;
while (! Thread.CurrentThread (). isinterrupted ()) {
Queue.put (P = p.nextprobableprime ());
Thread.CurrentThread (). Sleep (10);
System.out.println (Queue.size ());
}
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.out.println ("Interrupted, please handle interrupt");
}
}
public static void Main (string[] args) {
Blockingqueue<biginteger> queue=new arrayblockingqueue<biginteger> (999999);
Thread Th1=new Thread (new Primeproducer (queue));
Th1.start ();
try {
Thread.CurrentThread (). Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Th1.interrupt ();
}
}
The interrupt mechanism is interrupted in this way, and there are two locations in the code that can check for interrupts: Thread.CurrentThread (). isinterrupted () method and blocking method put call.
Note that the second mechanism of the Run method can also be written in the following pattern, the interrupt exception handling in the while loop body, but must be in the exception handling catch statement block plus Thread.CurrentThread (). Interrupt () to reset the interrupt state, Otherwise, you may not be able to terminate the task. This is because the blocking library method clears the interrupt state before the interruptexception exception is thrown when a thread break is checked. Causes the while to return False when judging Thread.CurrentThread (). isinterrupted (), the thread does not exit, so the interrupt needs to be reset in the exception handling block.
public void Run () {
TODO auto-generated Method Stub
BigInteger P=biginteger.one;
while (! Thread.CurrentThread (). isinterrupted ()) {
try {
Queue.put (P=p.nextprobableprime ());
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.out.println ("Interrupted, please handle interrupt");
Thread.CurrentThread (). interrupt (); Resume interrupt state after capturing interruptedexception
}
Thread.CurrentThread (). Sleep (10);
System.out.println (Queue.size ());
}
}
Resources
"Java Concurrency in practice" Brian Goetz
Cancellation of thread threads in Java