1
2
3
4
5
6 package nl.tudelft.simulation.language.concurrent;
7
8 import java.util.logging.Logger;
9
10 /***
11 * The WorkerThread is a working thread. The thread sleeps while not interrupted. If interuppted the jon.run
12 * operation is invoked.
13 * <p>
14 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
15 * Netherlands.
16 * <p>
17 * See for project information <a
18 * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
19 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
20 * </a>, no warranty
21 *
22 * @version $Revision: 1.9 $ $Date: 2005/08/04 12:08:55 $
23 * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>, <a
24 * href="mailto:a.verbraeck@tbm.tudelft.nl">Alexander Verbraeck </a>
25 */
26 public class WorkerThread extends Thread
27 {
28 /*** the job to execute. */
29 private Runnable job = null;
30
31 /*** finalized. */
32 private boolean finalized = false;
33
34 /***
35 * constructs a new SimulatorRunThread.
36 *
37 * @param name
38 * the name of the thread
39 * @param job
40 * the job to run
41 */
42 public WorkerThread(final String name, final Runnable job)
43 {
44 super(name);
45 this.job = job;
46 this.setDaemon(false);
47 this.setPriority(Thread.NORM_PRIORITY);
48 this.start();
49 }
50
51 /***
52 * @see java.lang.Object#finalize()
53 */
54 @Override
55 public final synchronized void finalize()
56 {
57 this.finalized = true;
58 try
59 {
60 super.finalize();
61 }
62 catch (Throwable exception)
63 {
64 Logger.getLogger("nl.tudelft.simulation.language.concurrent").warning(exception.getMessage());
65 exception.printStackTrace();
66 }
67 }
68
69 /***
70 * @see java.lang.Runnable#run()
71 */
72 @Override
73 public final synchronized void run()
74 {
75 while (!this.finalized)
76 {
77 try
78 {
79 this.wait();
80 }
81 catch (InterruptedException interruptedException)
82 {
83 this.interrupt();
84 try
85 {
86 this.job.run();
87 }
88 catch (Exception exception)
89 {
90 Logger.getLogger("nl.tudelft.simulation.language.concurrent").severe(
91 exception.getMessage());
92 exception.printStackTrace();
93 }
94 Thread.interrupted();
95 }
96 }
97 }
98 }