I have a fixed pool with a single thread. When I submit new task I want to stop all old threads except last one.
private class MyPool extends ThreadPoolExecutor {
    public MyPool(long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(1, 1, keepAliveTime, unit, workQueue);
    }
    public boolean isReady() {
        return semaphore;
    }
    @Override
    public <T> Future<T> submit(Callable<T> task) {
        // Iterate all existed task and stop
        Future<T> future = super.submit(task);
        return future;
    }
    private volatile boolean semaphore;
}
Code of running task:
private class MyTask implements Runnable {
    private volatile boolean isRun = true;
    private int id;
    public MyTask(int id) {
        this.id = id;
    }
    public void stop() {
        isRun = false;
    }
    @Override
    public void run() {
        try {
            System.out.println("Start " + id);
            if (isRun) {
                Thread.sleep(1000);
                System.out.println("Stop " + id);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }       
}
I created my own class, but It doesn't correctly work because semaphore effects on a new task as well. What is the best way to do it?
 
     
    