Thibstars

MonitorThread

Nov 15th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import org.apache.log4j.Logger;
  2.  
  3. import java.util.concurrent.ThreadPoolExecutor;
  4.  
  5. /**
  6.  * Thread monitoring its' ThreadPoolExecutor.
  7.  *
  8.  * @author Thibault Helsmoortel
  9.  */
  10.  
  11. public class MonitorThread implements Runnable {
  12.     private static final Logger LOGGER = Logger.getLogger(MonitorThread.class);
  13.  
  14.     private ThreadPoolExecutor executor;
  15.     private int seconds;
  16.     private boolean run = true;
  17.  
  18.     /**
  19.     * Class constructor specifying executor and delay.
  20.     */
  21.     public MonitorThread(ThreadPoolExecutor executor, int delay) {
  22.         this.executor = executor;
  23.         this.seconds = delay;
  24.     }
  25.  
  26.     /**
  27.     * Shuts down this Thread.
  28.     */
  29.     public void shutdown() {
  30.         this.run = false;
  31.     }
  32.  
  33.     @Override
  34.     public void run() {
  35.         while (run) {
  36.             LOGGER.debug(
  37.                     String.format(
  38.                             "[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
  39.                             this.executor.getPoolSize(),
  40.                             this.executor.getCorePoolSize(),
  41.                             this.executor.getActiveCount(),
  42.                             this.executor.getCompletedTaskCount(),
  43.                             this.executor.getTaskCount(),
  44.                             this.executor.isShutdown(),
  45.                             this.executor.isTerminated()));
  46.             try {
  47.                 Thread.sleep(seconds * 1000);
  48.             } catch (InterruptedException e) {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment