Thibstars icon

MonitorThread

Thibstars | PRO | 11/15/16 01:14:51 PM UTC | 0 ⭐ | 282 👁️ | Never ⏰ | []
Java |

1.55 KB

|

None

|

0 👍

/

0 👎

import org.apache.log4j.Logger;
 
import java.util.concurrent.ThreadPoolExecutor;
 
/**
 * Thread monitoring its' ThreadPoolExecutor.
 *
 * @author Thibault Helsmoortel
 */
 
public class MonitorThread implements Runnable {
    private static final Logger LOGGER = Logger.getLogger(MonitorThread.class);
 
    private ThreadPoolExecutor executor;
    private int seconds;
    private boolean run = true;
 
    /**
    * Class constructor specifying executor and delay.
    */
    public MonitorThread(ThreadPoolExecutor executor, int delay) {
        this.executor = executor;
        this.seconds = delay;
    }
 
    /**
    * Shuts down this Thread.
    */
    public void shutdown() {
        this.run = false;
    }
 
    @Override
    public void run() {
        while (run) {
            LOGGER.debug(
                    String.format(
                            "[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                            this.executor.getPoolSize(),
                            this.executor.getCorePoolSize(),
                            this.executor.getActiveCount(),
                            this.executor.getCompletedTaskCount(),
                            this.executor.getTaskCount(),
                            this.executor.isShutdown(),
                            this.executor.isTerminated()));
            try {
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
 
    }
}

Comments