forked from kohsuke/com4j
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTask.java
More file actions
107 lines (93 loc) · 2.65 KB
/
Task.java
File metadata and controls
107 lines (93 loc) · 2.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com4j;
import java.util.concurrent.Callable;
/**
* Used to execute a chunk of code from {@link ComThread}.
*
* @param <T> the type of the return value of the {@link #execute()} and {@link #execute(ComThread)} methods
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
abstract class Task<T> implements Callable<T> {
private volatile boolean done = false;
public abstract T call();
private static ThreadLocal<ComThread> comThread = new ThreadLocal<ComThread>();
/**
* Returns the current ComThread the task is running in.
* Only valid while running a Task.
*/
public static ComThread getComThread() {
return null == comThread ? null : comThread.get();
}
/**
* Executes the task in a {@link ComThread}
* @return the return value of the Task execution (returned by {@link #call()}).
*/
public final T execute() {
return execute(ComThreadMulti.get());
}
/**
* Executes the task in the given {@link ComThread}
* @param t the ComThread to execute the task
* @return the return value of the Task execution (returned by {@link #call()}).
*/
public final T execute(ComThread t) {
comThread.set(t);
try {
T result;
if(Thread.currentThread()==t)
// if invoked from within ComThread, execute it at once
result = call();
else
// otherwise schedule the execution and block
result = t.execute(this);
comThread.set(null);
return result;
}
catch (RuntimeException e) {
comThread.set(null);
throw e;
}
}
/**
* Called from {@link ComThread} to run the task.
*/
final synchronized void invoke() {
result = null;
exception = null;
try {
result = call();
} catch( Throwable e ) {
exception = e;
} finally {
done = true;
}
// let the calling thread know that we are done.
notify();
}
/**
* Indicates whether this task is done executing
* @return {@literal true} if execution of the task is finished
*/
final boolean isDone() {
return done;
}
/**
* Prepare for invocation.
*/
final void reset() {
done = false;
}
/**
* Managed by {@link ComThread} to pass the return value
* across threads.
*/
T result;
/**
* Managed by {@link ComThread} to pass the exception
* across threads.
*/
Throwable exception;
/**
* TODO: do we need this field at all?
*/
Error error;
}