GraphUpdater.java

  1. package org.opentrafficsim.draw.graphs;

  2. import java.util.concurrent.BlockingQueue;
  3. import java.util.concurrent.LinkedBlockingQueue;
  4. import java.util.concurrent.TimeUnit;

  5. import org.djutils.logger.CategoryLogger;

  6. /**
  7.  * The GraphUpdater can be used to repeatedly offer a value that is automatically processed in order of offering in a parallel
  8.  * Thread.
  9.  * <p>
  10.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  12.  * </p>
  13.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  15.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  16.  * @param <T> type of value in queue
  17.  */
  18. public class GraphUpdater<T>
  19. {

  20.     /** Queue of update times for executing Thread. */
  21.     private final BlockingQueue<T> queue = new LinkedBlockingQueue<>();

  22.     /**
  23.      * Constructs and starts a thread that performs each given task from a queue.
  24.      * @param workerName name for the working thread
  25.      * @param invokingThread invoking thread, the worker will stop when this thread is interrupted
  26.      * @param updater updater to perform with the queued value
  27.      */
  28.     public GraphUpdater(final String workerName, final Thread invokingThread, final Updater<T> updater)
  29.     {
  30.         new Thread(new Runnable()
  31.         {
  32.             @SuppressWarnings({"synthetic-access"})
  33.             @Override
  34.             public void run()
  35.             {
  36.                 while (!invokingThread.isInterrupted())
  37.                 {
  38.                     try
  39.                     {
  40.                         T t = GraphUpdater.this.queue.poll(5, TimeUnit.SECONDS);
  41.                         if (t != null)
  42.                         {
  43.                             updater.update(t);
  44.                         }
  45.                     }
  46.                     catch (InterruptedException exception)
  47.                     {
  48.                         CategoryLogger.always().error(exception, "Worker {} thread stopped.", workerName);
  49.                         break;
  50.                     }
  51.                 }
  52.             }
  53.         }, workerName).start();
  54.     }

  55.     /**
  56.      * Offer a next value to the queue.
  57.      * @param t next value to offer to the queue
  58.      */
  59.     public final void offer(final T t)
  60.     {
  61.         this.queue.offer(t);
  62.     }

  63.     /**
  64.      * Functional interface for updates to perform.
  65.      * <p>
  66.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  67.      * <br>
  68.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  69.      * </p>
  70.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  71.      * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  72.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  73.      * @param <T> type of value in queue
  74.      */
  75.     @FunctionalInterface
  76.     interface Updater<T>
  77.     {
  78.         /**
  79.          * Perform an update.
  80.          * @param t value to update by
  81.          */
  82.         void update(T t);
  83.     }

  84.     @Override
  85.     public String toString()
  86.     {
  87.         return "GraphUpdater [queue=" + this.queue + "]";
  88.     }

  89. }