top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we make sure main() is the last thread to finish in Java Program?

0 votes
2,474 views
How can we make sure main() is the last thread to finish in Java Program?
posted Feb 12, 2018 by Jon Deck

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

We can use Thread join() method to make sure all the threads created by the program is dead before finishing the main function.

Java Thread join method can be used to pause the current thread execution until unless the specified thread is dead. There are three overloaded join functions:

public final void join(): This java thread join method puts the current thread on wait until the thread on which it’s called is dead. If the thread is interrupted, it throws InterruptedException.

public final synchronized void join(long millis): This java thread join method is used to wait for the thread on which it’s called to be dead or wait for specified milliseconds. Since thread execution depends on OS implementation, it doesn’t guarantee that the current thread will wait only for given time.

public final synchronized void join(long millis, int nanos): This java thread join method is used to wait for thread to die for given milliseconds plus nanoseconds.

answer Feb 13, 2018 by Frank Lee
...