forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterrupting2.java
More file actions
55 lines (39 loc) · 1.13 KB
/
Interrupting2.java
File metadata and controls
55 lines (39 loc) · 1.13 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
package com.csdhsm.concurrent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class BlockedMutex{
private Lock lock = new ReentrantLock();
public BlockedMutex() {
//Acquire it right away,to demontrate interruption
//of a task blocked on a ReentrantLock
lock.lock();
}
public void f(){
try {
//This will never be available to a second task
lock.lockInterruptibly();//Special call
System.out.println("lock acquired in f()");
} catch (InterruptedException e) {
System.out.println("InterruptedException from lock acquisition in f()");
}
}
}
class Blocked2 implements Runnable{
BlockedMutex blocked = new BlockedMutex();
@Override
public void run() {
System.out.println("Waiting for f() in BlockedMutex()");
blocked.f();
System.out.println("Broken out of blocked call");
}
}
public class Interrupting2 {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new Blocked2());
t.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("Issuing t.interrupt()");
t.interrupt();
}
}