View Javadoc
1   package nl.tudelft.simulation.language.concurrent;
2   
3   import java.util.concurrent.atomic.AtomicBoolean;
4   
5   import org.djutils.logger.CategoryLogger;
6   
7   /**
8    * The WorkerThread is a working thread. The thread sleeps while not interrupted. If interrupted the job.run operation is
9    * invoked.
10   * <p>
11   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
13   * project is distributed under a three-clause BSD-style license, which can be found at
14   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
15   * </p>
16   * @author <a href="mailto:phmjacobs@hotmail.com">Peter H.M. Jacobs</a>
17   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18   */
19  
20  public class WorkerThread extends Thread
21  {
22      /** the job to execute. */
23      private Runnable job = null;
24  
25      /** finalized. */
26      private boolean finalized = false;
27  
28      /** running. */
29      private AtomicBoolean running = new AtomicBoolean(false);
30  
31      /**
32       * constructs a new SimulatorRunThread.
33       * @param name the name of the thread
34       * @param job the job to run
35       */
36      public WorkerThread(final String name, final Runnable job)
37      {
38          super(name);
39          this.job = job;
40          this.setDaemon(false);
41          this.setPriority(Thread.NORM_PRIORITY);
42          this.start();
43      }
44  
45      /**
46       * Clean up the worker thread. synchronized method, otherwise it does not own the Monitor on the wait.
47       */
48      public synchronized void cleanUp()
49      {
50          this.running.set(false);
51          this.finalized = true;
52          if (!this.isInterrupted())
53          {
54              this.notify(); // in case it is in the 'wait' state
55          }
56          this.job = null;
57      }
58  
59      /**
60       * @return whether the run method of the job is running or not
61       */
62      public synchronized boolean isRunning()
63      {
64          return this.running.get();
65      }
66  
67      @Override
68      public synchronized void run()
69      {
70          while (!this.finalized) // always until finalized
71          {
72              try
73              {
74                  this.wait(); // as long as possible
75              }
76              catch (InterruptedException interruptedException)
77              {
78                  if (!this.finalized)
79                  {
80                      this.interrupt(); // set the status to interrupted
81                      try
82                      {
83                          this.running.set(true);
84                          this.job.run();
85                          this.running.set(false);
86                      }
87                      catch (Exception exception)
88                      {
89                          CategoryLogger.always().error(exception);
90                      }
91                      Thread.interrupted();
92                  }
93              }
94          }
95      }
96  }