-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNativeTask.java
More file actions
executable file
·51 lines (47 loc) · 1.36 KB
/
NativeTask.java
File metadata and controls
executable file
·51 lines (47 loc) · 1.36 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
import java.io.IOException;
class NativeTask {
public NativeTask() {}
static {
System.loadLibrary("interrupt");
}
/**
* Do a long task (well, it doesn't really, of course, this is example code,
* but it pretends by sleeping for 25s). It is an error to call doTask() on
* more than one thread at once (ie, the calls must be strictly serialised).
*/
public void doTask()
throws InterruptedException {
long start = System.currentTimeMillis();
NativeTaskSelector selector = new NativeTaskSelector(this);
try {
selector.registerStart();
if (doTask0()) {
System.out.println("Task woken up...");
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
selector.registerEnd();
try { selector.close(); } catch (IOException impossible) {}
long elapsed = System.currentTimeMillis() - start;
System.out.println("doTask() took " + elapsed + "ms");
}
}
/**
* Wake up the NativeTask if it is inside doTask(), which will return
* immediately.
*/
public void wakeupTask() {
wakeupTask0();
}
/**
* Native implementation of wakeupTask().
*/
native private void wakeupTask0();
/**
* Native implementation of the task.
* @return true if the task was interrupted by a call to wakeupTask().
*/
native private boolean doTask0();
private long eventHandle;
}