1 |
package appl.parallel.thread; |
2 |
/** |
3 |
* Executes a method call in a single Thread |
4 |
* |
5 |
* @author Dominik Appl |
6 |
*/ |
7 |
public abstract class OneMethodThread implements Runnable { |
8 |
private volatile Thread thisThread; |
9 |
|
10 |
private final String threadName; |
11 |
|
12 |
private final int priority; |
13 |
|
14 |
private final Object[] parameters; |
15 |
|
16 |
protected Object executionResult; |
17 |
|
18 |
/** |
19 |
* Creates a new Thread. Do not forget to {@link #start()} it. |
20 |
* @param threadName a name for the thread (usefull while debugging) |
21 |
* @param priority a priority for the Thread |
22 |
* @param parameters the parameters may be used later in the run method by calling the {@link #getParameter(int)} method |
23 |
* |
24 |
*/ |
25 |
public OneMethodThread(String threadName, int priority, Object... parameters) { |
26 |
this.threadName = threadName; |
27 |
this.priority = priority; |
28 |
// TODO Auto-generated constructor stub |
29 |
this.parameters = parameters; |
30 |
} |
31 |
|
32 |
public abstract void run(); |
33 |
|
34 |
/** |
35 |
* Starts the thread |
36 |
*/ |
37 |
public void start() { |
38 |
thisThread = new Thread(this); |
39 |
thisThread.setName(threadName); |
40 |
thisThread.setPriority(priority); |
41 |
thisThread.start(); |
42 |
} |
43 |
|
44 |
/** |
45 |
* @param paraNo the position in the given parameter array |
46 |
* @return the parameter given in the constructor |
47 |
*/ |
48 |
public Object getParameter(int paraNo) { |
49 |
return parameters[paraNo]; |
50 |
} |
51 |
|
52 |
/** |
53 |
* waits until the encapulated Thread has finished |
54 |
* |
55 |
*/ |
56 |
public void join(){ |
57 |
try { |
58 |
thisThread.join(); |
59 |
} catch (InterruptedException e) { |
60 |
e.printStackTrace(); |
61 |
} |
62 |
} |
63 |
|
64 |
|
65 |
/** |
66 |
* the execution result must be explicitly set in the {@link #run()} method. |
67 |
* |
68 |
* @return the result |
69 |
*/ |
70 |
public Object getExecutionResult(){ |
71 |
return executionResult; |
72 |
} |
73 |
|
74 |
} |