Home  >  Article  >  Java  >  What is the difference between wait() and join() methods in java

What is the difference between wait() and join() methods in java

青灯夜游
青灯夜游Original
2018-12-14 15:28:399499browse

The difference between wait() and join() methods in java is that they exist in different java packages; the wait() method is used for inter-thread communication, and the thread in the wait state it imposes can be started; the join() method Used to add ordering between multiple threads, the wait it imposes cannot be broken.

What is the difference between wait() and join() methods in java

The wait() method in java

The function of the wait() method is to let the current thread To enter the waiting state, wait() will be used together with notify() and notifyAll() methods.

The notify() and notifyAll() methods are used to wake up waiting threads. The notify() method: wakes up a single thread, and the notifyAll() method: wakes up all threads.

The join() method in java

The join() method waits for this thread to end and complete its execution. Its main function is to synchronize, changing the execution between threads from "parallel" to "serial".

That is to say, when we call the join() method of thread B in thread A, the thread execution process changes: thread A must wait for thread B to complete execution before it can continue to execute.

Similarities between the wait() method and the join() method

1. The wait() and join() methods are both used for pauses The current thread in Java enters the waiting state.

2. In Java, you can call the interrupt() method to interrupt the thread status of wait() and join().

3, wait() and join() are all non-static methods.

4, wait() and join() are all overloaded in Java. wait() and join() have no timeout but accept a timeout parameter.

Although the wait() method and the join() method are similar, there are still differences between the wait() method and the join() method.

The difference between wait() method and join() method

What is the difference between wait() and join() methods in java

1 , exist in different java packages(The most obvious difference)

wait() method needs to be declared in the java.lang.Object class; while, the join() method is in java. Declared in the lang.Thread class.

2. Different purposes of use

The wait() method is used for inter-thread communication; and the join() method is used to add sorting between multiple threads. The two threads need to finish executing before the first thread can start executing.

3. Differences in waking up threads

We can start a thread that enters the waiting state through the wait() method by using the notify() and notifyAll() methods. But we cannot break the wait imposed by the join() method unless the thread that interrupted the connection has finished executing.

4. Synchronization context (The most important difference)

wait() method must be called from a synchronized (synchronized) context, that is, a synchronized block or method, otherwise it will Throws IllegalMonitorStateException.

However, we can call the join() method with or without a synchronized context in Java.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What is the difference between wait() and join() methods in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn