1

Scheduler gets the configuration from the server through new single thread. But application continues to execute even when the scheduler is executing the config() to get the configuration from the server.

App{
    XXXX;
    XXXX;
    try{
       scheduler()
    } catch(Exception e){

}

public void scheduler(){

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

        Callable<Void> callable = new Callable<Void>() {
            public Void call() throws Exception {
                try {
                    config();
                } finally {
                   service.schedule(this, TimeOut(), TimeUnit.SECONDS);
                }

                return null;

            }

        };
        service.schedule(callable, 0, TimeUnit.SECONDS);


}

How to make the main thread wait until the single thread getting the config completes.

MANOJ
  • 716
  • 4
  • 10
  • 29

3 Answers3

1

Try using ExecutorService#awaitTermination(time, unit) right after firing ExecutorService#shutdown():

ExecutorService#awaitTermination() blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

service.shutdown();
try {
    // Use Long.MAX_VALUE to wait forever, see link below
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
    // TODO ...
}

ExecutorService#awaitTermination()

Timing

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
0

In main thread call invoke wait on object in the scheduled thread call notify on the same object in which main thread invoked wait.

public class App {
    public Object lockObj = new Object();

    public void waitForNotifcation(){
        try{
            lockObj.wait();
        } catch( InterruptedException ex ) {
        }
    }
    public void scheduler(){

    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

            Callable<Void> callable = new Callable<Void>() {
                public Void call() throws Exception {
                    // TO DO yout code

                    // notify all the thread waiting for
                    lockObj.notifyAll();

                    return null;

                }

            };
            service.schedule(callable, 0, TimeUnit.SECONDS);


    }

    public static void main( String[] str ) {
        App app = new App();
        app.waitForNotifcation();
        app.scheduler();
    }
}
0

The answer to your question is create child thread inside the main method (main thread is responsible to execute main method in java) and then call join method using that created thread object. When join method will be called than the main method will stop its execution until the child thread complete its execution. Below call will help you to understand the above scenario.

public class ThreadClass extends Thread{

    public void run(){

        for(int i=0; i<3; i++){
            try {
                Thread.sleep(1000);
                System.out.println("i is "+ i);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args){
        ThreadClass t1 = new ThreadClass();
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("main thread will start its execution now");
    }
}

Output of the code will be : 
i is 0
i is 1
i is 2
main thread will start its execution now