为什么当前Thread在返回值之前不等待其他Threads?

时间:2021-09-12 20:40:44

I have two Threads. In each of them I'm running a scheduler.

我有两个主题。在他们每个人中我都在运行一个调度程序。

Thread one = new Thread(
    () -> scheduler.scheduleAtFixedRate(eventMaker, 0, 1, MICROSECONDS)
);

Thread two = new Thread(
    () -> timeChecker.schedule(timeAnalyser, 1, TimeUnit.SECONDS)
);

Then I start them and invoke join() on them to make the method, in which they are initialized, wait for them to finish before returning the return value.

然后我启动它们并在它们上调用join()以使初始化它们的方法在返回返回值之前等待它们完成。

threads = new Thread[2];
threads[0] = one;
threads[1] = two;

for (int i = 0; i < threads.length; i++) {
    threads[i].start();
}

for (int i = 0; i < threads.length; i++) {
    threads[i].join();
}

return a;

But still the method in which they are invoked doesn't wait for them. It returns instantly the value despite of the join() invokes. How can I achieve waiting for the Threads before returning?

但是仍然调用它们的方法不会等待它们。尽管join()调用,它立即返回值。如何才能在返回之前等待线程?

Your help would be appreciated.

非常感谢您的帮助。

1 个解决方案

#1


5  

Assuming scheduler and timeChecker are ScheduledExecutorServices, your code is doing what it should be doing.

假设scheduler和timeChecker是ScheduledExecutorServices,那么你的代码正在做它应该做的事情。

Both schedule and scheduleAtFixedRate are asynchronous in that they schedule the work to be done on a separate thread at some time in the future. Both of them return immediately.

schedule和scheduleAtFixedRate都是异步的,因为它们在将来的某个时间安排在单独的线程上完成工作。他们俩都立刻回来了。

Your two threads, one and two, are kind of pointless here. You seem to want to get the ScheduledFuture from the schedule calls and call get on them.

你的两个线程,一个和两个,在这里毫无意义。您似乎想从计划调用中获取ScheduledFuture并调用它们。

ScheduledFuture<?> schedulerFuture = scheduler.scheduleAtFixedRate(eventMaker, 0, 1, MICROSECONDS);
ScheduledFuture<?> timerFuture = timeChecker.schedule(timeAnalyser, 1, TimeUnit.SECONDS);

schedulerFuture.get();
timerFuture.get();

The ScheduledFuture returned for the call to schedule will return from get once the first execution is completed.

调度调度返回的ScheduledFuture将在第一次执行完成后从get返回。

The ScheduledFuture returned for the call to scheduleAtFixedRate will return from get only if it is cancelled or if the ScheduledExecutorService is terminated.

为调用scheduleAtFixedRate而返回的ScheduledFuture仅在取消时才返回get,或者如果ScheduledExecutorService被终止则返回。

Otherwise, the task will only terminate via cancellation or termination of the executor.

否则,任务将仅通过取消或终止执行者来终止。

#1


5  

Assuming scheduler and timeChecker are ScheduledExecutorServices, your code is doing what it should be doing.

假设scheduler和timeChecker是ScheduledExecutorServices,那么你的代码正在做它应该做的事情。

Both schedule and scheduleAtFixedRate are asynchronous in that they schedule the work to be done on a separate thread at some time in the future. Both of them return immediately.

schedule和scheduleAtFixedRate都是异步的,因为它们在将来的某个时间安排在单独的线程上完成工作。他们俩都立刻回来了。

Your two threads, one and two, are kind of pointless here. You seem to want to get the ScheduledFuture from the schedule calls and call get on them.

你的两个线程,一个和两个,在这里毫无意义。您似乎想从计划调用中获取ScheduledFuture并调用它们。

ScheduledFuture<?> schedulerFuture = scheduler.scheduleAtFixedRate(eventMaker, 0, 1, MICROSECONDS);
ScheduledFuture<?> timerFuture = timeChecker.schedule(timeAnalyser, 1, TimeUnit.SECONDS);

schedulerFuture.get();
timerFuture.get();

The ScheduledFuture returned for the call to schedule will return from get once the first execution is completed.

调度调度返回的ScheduledFuture将在第一次执行完成后从get返回。

The ScheduledFuture returned for the call to scheduleAtFixedRate will return from get only if it is cancelled or if the ScheduledExecutorService is terminated.

为调用scheduleAtFixedRate而返回的ScheduledFuture仅在取消时才返回get,或者如果ScheduledExecutorService被终止则返回。

Otherwise, the task will only terminate via cancellation or termination of the executor.

否则,任务将仅通过取消或终止执行者来终止。