Examveda

Which of this method can be used to make the main thread to be executed last among all the threads?

A. stop()

B. sleep()

C. join()

D. call()

Answer: Option C

Solution (By Examveda Team)

The join() method in Java is used to pause the execution of the current thread until the thread on which join() is called has finished executing.

To ensure that the main thread executes last, we can call the join() method on all other threads in the main thread.

This causes the main thread to wait for the completion of other threads before it proceeds.

sleep() is used to pause a thread for a specific time, not to coordinate the order of thread completion.

stop() is deprecated and unsafe.

call() is not a valid method for controlling thread execution in this context.

This Question Belongs to Java Program >> Threads

Join The Discussion

Comments (1)

  1. Ganesh Commn
    Ganesh Commn:
    7 months ago

    The join() method is specifically designed to make one thread wait for another thread to complete its execution. Here's why it's the correct choice:
    How join() works:

    When you call thread.join() from the main thread, the main thread will pause its execution

    The main thread will wait until the specified thread completes its execution

    Only after all joined threads complete will the main thread continue

Related Questions on Threads

What is a thread in Java?

A. A lightweight process that runs independently within a program

B. A data structure to store variables

C. A type of loop

D. A synchronization mechanism