OnJava8-Examples/threads/CloseResource.java
2016-07-05 14:46:17 -06:00

46 lines
1.4 KiB
Java

// threads/CloseResource.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Interrupting a blocked task by
// closing the underlying resource
// {TimeOutDuringTesting}
import java.net.*;
import java.util.concurrent.*;
import java.io.*;
public class CloseResource {
public static void
main(String[] args) throws Exception {
ExecutorService es = Executors.newCachedThreadPool();
ServerSocket server = new ServerSocket(8080);
try(InputStream socketInput =
new Socket("localhost", 8080)
.getInputStream()) {
es.execute(new IOBlocked(socketInput));
es.execute(new IOBlocked(System.in));
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Shutting down all threads");
es.shutdownNow();
TimeUnit.SECONDS.sleep(1);
System.out.println(
"Closing " + socketInput.getClass().getName());
socketInput.close(); // Releases blocked thread
}
TimeUnit.SECONDS.sleep(1);
System.out.println(
"Closing " + System.in.getClass().getName());
System.in.close(); // Releases blocked thread
}
}
/* Output:
Waiting for read():
Waiting for read():
Shutting down all threads
Closing java.net.SocketInputStream
Interrupted from blocked I/O
Exiting IOBlocked.run()
Closing java.io.BufferedInputStream
Exiting IOBlocked.run()
*/