Which of the following methods can make the main thread wait until the child thread call completes?
时间: 2024-03-25 13:37:43 浏览: 78
The `join()` method can make the main thread wait until the child thread call completes. When a thread calls the `join()` method on another thread, the calling thread will be blocked until the other thread completes its execution. For example:
```java
Thread childThread = new Thread(() -> {
// some time-consuming task
});
childThread.start();
try {
childThread.join(); // main thread will wait until child thread completes
} catch (InterruptedException e) {
// handle exception
}
```
In the above example, the main thread calls the `join()` method on the `childThread` object. This will block the main thread until the child thread completes its execution. Once the child thread completes, the main thread will resume its execution.
阅读全文