-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefiantStream.java
More file actions
64 lines (54 loc) · 1.69 KB
/
Copy pathDefiantStream.java
File metadata and controls
64 lines (54 loc) · 1.69 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
package threadbook.ch15;
import java.io.*;
public class DefiantStream extends Object {
public static void main(String[] args) {
final InputStream in = System.in;
Runnable r = new Runnable() {
public void run() {
try {
System.err.println(
"about to try to read from in");
in.read();
System.err.println("just read from in");
} catch ( InterruptedIOException iiox ) {
iiox.printStackTrace();
} catch ( IOException iox ) {
iox.printStackTrace();
//} catch ( InterruptedException ix ) {
// InterruptedException is never thrown!
// ix.printStackTrace();
} catch ( Exception x ) {
x.printStackTrace();
} finally {
Thread currThread =
Thread.currentThread();
System.err.println("inside finally:\n" +
" currThread=" + currThread + "\n" +
" currThread.isAlive()=" +
currThread.isAlive());
}
}
};
Thread t = new Thread(r);
t.start();
try { Thread.sleep(2000); }
catch ( InterruptedException x ) { }
System.err.println("about to interrupt thread");
t.interrupt();
System.err.println("just interrupted thread");
try { Thread.sleep(2000); }
catch ( InterruptedException x ) { }
System.err.println("about to stop thread");
// stop() is being used here to show that the extreme
// action of stopping a thread is also ineffective.
// Because stop() is deprecated, the compiler issues
// a warning.
t.stop();
System.err.println("just stopped thread, t.isAlive()=" +
t.isAlive());
try { Thread.sleep(2000); }
catch ( InterruptedException x ) { }
System.err.println("t.isAlive()=" + t.isAlive());
System.err.println("leaving main()");
}
}