使主线程等到新的单线程在java中完全执行

时间:2018-01-09 04:17:35

标签: java multithreading java-ee-6 schedule

Scheduler通过新的单线程从服务器获取配置。但是,即使调度程序正在执行config()以从服务器获取配置,应用程序也会继续执行。

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);


}

如何使主线程等到单线程获取配置完成。

3 个答案:

答案 0 :(得分:0)

点击ExecutorService#awaitTermination(time, unit)后立即尝试使用ExecutorService#shutdown()

  

ExecutorService#awaitTermination()阻塞,直到所有任务在关闭请求或超时发生后完成执行,或者当前线程被中断,以先发生者为准。

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

答案 1 :(得分:0)

在主线程调用中调用等待调度线程调用中的对象,调用主线程调用的同一对象等待。

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();
    }
}

答案 2 :(得分:0)

你的问题的答案是在main方法中创建子线程(主线程负责在java中执行main方法),然后使用创建的线程对象调用join方法。当调用join方法时,main方法将停止执行,直到子线程完成其执行。 以下电话将帮助您了解上述情况。

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