Java High concurrency
Problem Description: Interrupt of current if judgment ' if in sleep, even if outside interruption, inside will also orderly exit,
But when the if judge is behind the sleep, he will want to interrupt the exception (not enough time to cause main is also a thread)
Package com.example.echo.Lock; /** * When Thread writes where, when the current process affects//** */class Interrupted_root_make extends thread{@Override public void R
Un () {while (true) {System.out.println ("-----"); /** * Use the interrupt command, the outside is only a notification command, the actual interruption of the operation or the need to judge the internal operation, exit/try {Thread.s
Leep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
} if (Thread.CurrentThread (). isinterrupted ()) {System.out.println ("I has interputed");
Break
} Thread.yield ();
}} public class Interruped_test {public static void main (string[] args) throws Interruptedexception {
Interrupted_root_make interrupted_root_make = new Interrupted_root_make ();
Interrupted_root_make.start ();
Interrupted_root_make.interrupt (); }
}
Right to
Class Interrupted_root_make extends thread{
@Override public
void Run () {while
(true) {
if ( Thread.CurrentThread (). isinterrupted ()) {
System.out.println ("I has interputed");
break;
SYSTEM.OUT.PRINTLN ("-----");
/**
* Use the interrupt command, the outside is only a notification command, the actual interruption of the operation or the need to internal judgment operation, exit/
try {
thread.sleep;
} catch ( Interruptedexception e) {
e.printstacktrace ();
}
Thread.yield ();
}} public class Interruped_test {public
static void Main (string[] args) throws Interruptedexception {
Interrupted_root_make interrupted_root_make = new Interrupted_root_make ();
Interrupted_root_make.start ();
Thread.Sleep (1000);
Interrupted_root_make.interrupt ();
}