Search code examples
javamultithreadingscheduled-tasksscheduledexecutorservice

Stop ScheduledThreadPoolExecutor from innerTask


So I have a Thread wherein ScheduledThreadPoolExecutor is created with periodic Task, so I want to stop my ScheduledThreadPoolExecutor from Task when condition occurs.

After that from Thread wherein ScheduledThreadPoolExecutor is existed to make a notify to another Thread. Perhaps I did something wrong, I cannot to send notify from InnerThread to parent Thread Buyer. After that from Buyer sending another notify to MasterContainer.

How can I do this?

import java.util.Date;
import java.util.concurrent.*;

public class Buyer implements Runnable {

    private CommonObj cmnObj;

    public Buyer(CommonObj msg) {
        this.cmnObj = cmnObj;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + " is starting");

        ScheduledThreadPoolExecutor sch = (ScheduledThreadPoolExecutor)
                Executors.newScheduledThreadPool(1);
        sch.setRemoveOnCancelPolicy(true);

        FutureRunnable periodicTask = new FutureRunnable() {

            @Override
            public void run() {
                try {
                    System.out.println("\t periodicTask Execution Time: "
                            + ScheduledExample.fmt.format(new Date()));

                    try {
                        Thread.sleep(2 * 1000);

                        synchronized (this) {
                            System.out.println("\t periodicTask need to close: "
                                    + ScheduledExample.fmt.format(new Date()));

                            this.getFuture().cancel(true);
                            System.out.println("\t periodicTask cancelled: "
                                    + ScheduledExample.fmt.format(new Date()));
                            this.notify();
                            return;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("\t periodicTask End Time: "
                            + ScheduledExample.fmt.format(new Date()));
                } catch (Exception e) {

                }
            }
        };

        Future<?> periodicFuture = sch.scheduleAtFixedRate(periodicTask, 3, 3, TimeUnit.SECONDS);
        periodicTask.setFuture(periodicFuture);
        synchronized (sch) {
            try {
                System.out.println(name + " is before wait");
                sch.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " is before notify");

            this.notify();
        }

        System.out.println(name + " is ended");


    }



}

abstract class FutureRunnable implements Runnable {
    private Future<?> future;

    public Future<?> getFuture() {
        return future;
    }

    public void setFuture(Future<?> future) {
        this.future = future;
    }
}

Solution

  • In your code, your inner task syncronized on periodicTask and outer syncronized on sch, this does not work.

    If you want to syncronize inner and outer thread, you should syncronize on the same object, as well as call wait and notify on the same object.