----------------------------------------------------
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;
/**
* 中断
*
* @author lenovo
*
*/
class SleepBlocked implements Runnable {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(100);
} 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;
}
@Override
public 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) {//不释放锁
Thread.yield();
}
}
public SynchronizedBlocked() {
new Thread() {
public void run() {
f();// 这个线程锁了
}
}.start();
}
@Override
public void run() {
System.out.println("试图调用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 running
System.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:org.rui.thread.concurrency.SleepBlocked
interrupt sent to org.rui.thread.concurrency.SleepBlocked
InterruptedException
-----------------------
Exiting SleepBlocked.run()
waiting for read();
interrupting:org.rui.thread.concurrency.IOBlocked
interrupt sent to org.rui.thread.concurrency.IOBlocked
-----------------------
试图调用f()
interrupting:org.rui.thread.concurrency.SynchronizedBlocked
interrupt 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;
/**
* 关闭任务在其上发生阻塞的底层资源
*
* @author lenovo
*
*/
public class CloseResource {
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
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 down all threads");
exec.shutdownNow();
TimeUnit.MILLISECONDS.sleep(1);
System.out.println("closing " + is.getClass().getName());
is.close();// releases blocked thread
TimeUnit.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;
/**
* 被阻塞的的nio 通道会自动地响应中断
*
* @author lenovo
*
*/
class NIOBlocked implements Runnable {
private final SocketChannel sc;
public NIOBlocked(SocketChannel sc) {
this.sc = sc;
}
@Override
public 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);
//通过取消产生一个中断
f.cancel(true);
TimeUnit.MILLISECONDS.sleep(1);
//release the block by closing the channel 释放块通过关闭通道
sc2.close();
}
}
/**
* output:
waiting for read:org.rui.thread.concurrency.NIOBlocked@fced4
waiting for read:org.rui.thread.concurrency.NIOBlocked@1d3cdaa
ClosedByInterruptException
exiting nioblocked.run() org.rui.thread.concurrency.NIOBlocked@fced4
AsynchronousCloseException
exiting nioblocked.run() org.rui.thread.concurrency.NIOBlocked@1d3cdaa
*/