-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloseResource.java
More file actions
65 lines (61 loc) · 2.01 KB
/
CloseResource.java
File metadata and controls
65 lines (61 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.core.concurrent;
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;
/**
* Interrupting a blocked task by closing the underlying resource.
* {RunByHand}
*/
/**
* @Package:cn.ucaner.core.concurrent
* @ClassName:CloseResource
* @Description: <p> CloseResource </p>
* @Author: - jason
* @CreatTime:2018年5月16日 下午6:11:46
* @Modify By:
* @ModifyTime: 2018年5月16日
* @Modify marker:
* @version V1.0
*/
public class CloseResource {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
@SuppressWarnings("unused")
ServerSocket server = new ServerSocket(8080);
InputStream socketInput = new Socket("localhost", 8080).getInputStream();
exec.execute(new IOBlocked(socketInput));
exec.execute(new IOBlocked(System.in));
TimeUnit.MILLISECONDS.sleep(100);
System.out.println("Shutting down all threads");
exec.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: (85% match)
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()
*///:~