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