----------------------------------------------------
Package Org.rui.thread.concurrency;import Java.io.ioexception;import Java.io.inputstream;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.Future; Import java.util.concurrent.timeunit;/** * Interrupt * * @author Lenovo * */class sleepblocked implements Runnable {@Overridepu Blic void Run () {try {TimeUnit.SECONDS.sleep],} catch (Interruptedexception e) {System.out.println (" Interruptedexception ");} System.out.println ("Exiting Sleepblocked.run ()");}} Class Ioblocked implements Runnable {private InputStream in;public ioblocked (InputStream is) {in = is;} @Overridepublic void Run () {System.out.println ("Waiting for Read ();"); try {in.read ();} catch (IOException e) {if (Thread.CurrentThread (). isinterrupted ()) {System.out.println ("interrupted From blocked I/O ");} else {}e.printstacktrace ();}}} Class Synchronizedblocked implements Runnable {public synchronized void F () {while (true) {//Do not release lock Thread.yi Eld ();}} Public SynchroniZedblocked () {new Thread () {public void run () {f ();//This thread locks}}.start ();} @Overridepublic void Run () {System.out.println ("attempted to invoke F ()"); F (); System.out.println ("Exiting synchroniedblocked. Run ()");}} -------------------------------------------public class Interrupting {private static executorservice EXE = Executors.newcachedthreadpool (); static void Test (Runnable R) throws interruptedexception {future<?> f = Exe.submit (R); TimeUnit.MILLISECONDS.sleep (100); System.out.println ("interrupting:" + r.getclass (). GetName ()); F.cancel (true);//Intrrupts if RUNNINGSYSTEM.OUT.PRINTLN ("Interrupt sent to" + R.getclass (). GetName ());} public static void Main (string[] args) throws Interruptedexception{test (new sleepblocked ()); System.out.println ("-----------------------"); Test (new ioblocked (system.in)); System.out.println ("-----------------------"); Test (new synchronizedblocked ()); System.out.println ("-----------------------"); TimeUnit.MILLISECONDS.sleep (3); System.out.println ("Aborting with System.exit (0)"); System.exit (0);//since last 2 interrupts failed}}/** * Output:interrupting:o Rg.rui.thread.concurrency.SleepBlockedinterrupt sent to Org.rui.thread.concurrency.SleepBlockedInterruptedException-----------------------Exiting Sleepblocked.run () Waiting for Read (), Interrupting:org.rui.thread.concurrency.IOBlockedinterrupt sent to Org.rui.thread.concurrency.IOBlocked-----------------------attempted to invoke F () Interrupting:o Rg.rui.thread.concurrency.SynchronizedBlockedinterrupt sent to Org.rui.thread.concurrency.SynchronizedBlocked-----------------------aborting with system.exit (0) */
Package Org.rui.thread.concurrency;import Java.io.ioexception;import Java.io.inputstream;import Java.net.serversocket;import Java.net.socket;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.timeunit;/** * Close the underlying resource on which the task has been blocked * * @author Lenovo * */publi C class Closeresource {public static void main (string[] args) throws Exception {Executorservice exec = executors.newcached ThreadPool (); ServerSocket Server = new ServerSocket (8080); InputStream is = new Socket ("localhost", 8080). getInputStream (); Exec.execute (New ioblocked (IS)); Exec.execute (new ioblocked (system.in)); TimeUnit.MILLISECONDS.sleep (100); System.out.println ("Shutting all Threads"); Exec.shutdownnow (); TimeUnit.MILLISECONDS.sleep (1); System.out.println ("Closing" + Is.getclass (). GetName ()); Is.close ();//Releases Blocked ThreadTimeUnit.MILLISECONDS.sleep (1); System.out.println ("Closing" + System.in.getClass (). GetName ()); System.in.close ();//releases blocked thread}}
Package Org.rui.thread.concurrency;import Java.io.ioexception;import Java.net.inetsocketaddress;import Java.net.serversocket;import Java.net.socket;import Java.net.socketaddress;import Java.net.SocketOption;import Java.nio.bytebuffer;import Java.nio.channels.asynchronouscloseexception;import Java.nio.channels.closedbyinterruptexception;import Java.nio.channels.socketchannel;import Java.util.Set;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.Future; Import java.util.concurrent.timeunit;/** * The blocked NIO channel will automatically respond to interrupts * * @author Lenovo * */class Nioblocked implements Runnab Le {private final Socketchannel sc;public nioblocked (Socketchannel sc) {This.sc = SC;} @Overridepublic void Run () {System.out.println ("Waiting for read:" + this); try {sc.read (bytebuffer.allocate (1));} catch (Closedbyinterruptexception e) {System.out.println ("closedbyinterruptexception");} catch ( Asynchronouscloseexception e) {System.out.println ("AsynchronousclosEexception ");} catch (IOException e) {e.printstacktrace ();} System.out.println ("Exiting Nioblocked.run ()" + This);}} public class Niointerruption {public static void main (string[] args) throws Ioexception,interruptedexception { Executorservice exec = Executors.newcachedthreadpool (); ServerSocket Server = new ServerSocket (8080); Inetsocketaddress isa = new Inetsocketaddress ("localhost", 8080); Socketchannel SC1 = Socketchannel.open (ISA); Socketchannel SC2 = Socketchannel.open (ISA); future<?> f = exec.submit (new nioblocked (SC1)); Exec.execute (new nioblocked (SC2)); Exec.shutdown (); TimeUnit.MILLISECONDS.sleep (1);//Cancel generates an interrupt F.cancel (true); TimeUnit.MILLISECONDS.sleep (1);//release The block by closing the channel release block by closing channels Sc2.close ();}} /** * output:waiting for read:[email protected]waiting for read:[email protected] Closedbyinterruptexceptionexiting Nioblocked.run () [email protected]asynchronouscloseexceptionexiting Nioblocked.run () [email protected]*/
Interrupt explanation for Java thread termination when blocking---thinking in java4