The wait() and join() methods are used to pause the current thread. The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.
wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free. On the other hand join() is used for waiting a thread to die.
wait() 和 join() 之间的相似之处
- 方法 wait() 和 join() 都用于暂停Java的当前线程。
- 在Java, wait() 和 join() 都可以通过调用 interrupt() 方法来中断。
- wait() 和 join() 都是非静态方法。
- wait() 和 join() 在Java都是重载的。 wait() 和 join() 没有超时以及接受超时参数。
wait() 和 join() 方法的区别
- 最明显的区别是,两者都存在不同的包,wait() 方法在Java.lang.Object 类中声明,而 join() 方法在Java.lang.Thread 类中声明。
- wait() 用于线程间通信,join() 用于在多个线程之间添加排序,一个线程在第一个线程执行完成后开始执行。
- 我们可以通过使用 notify() 和 notifyAll() 方法启动一个等待线程(通过调用 wait() 进入这个状态)但是我们不能打破 join 强加的等待,除非或中断调用 join 的线程已经执行完成的。
- wait() 和 join() 之间最重要的区别是 wait() 必须从同步上下文中调用,即同步块或方法,否则它会抛出 IllegalMonitorStateException 但另一方面,我们可以在有和没有同步的情况下调用 join() 方法Java的上下文。
连接示例
// Java program to explain the use of join() class A extends Thread { @Override public void run() { for (int i = 1; i <= 4; i++) { try { Thread.sleep(100); } catch (Exception e) { System.out.println(e); } System.out.print(i + " "); } } } class B extends Thread { @Override public void run() { for (char i = 'a'; i <= 'd'; i++) { try { Thread.sleep(100); } catch (Exception e) { System.out.println(e); } System.out.print(i + " "); } } } class GFG extends Thread { public static void main(String args[]) { // creating two threads A a1 = new A(); B b1 = new B(); // starts second thread after when // first thread a1 is died. a1.start(); try { a1.join(); } catch (Exception e) { System.out.println(e); } // after thread a1 execution finished // then b1 thread start b1.start(); } }
输出:
1 2 3 4 a b c d
等待示例
// Java program to explain the // concept of Waiting a thread. class PNBCustomer { // let a initial amount is 9000 int amount = 9000; // synchronized function because i wil use for // waiting thread. Here synchronized means inter- // thread communication synchronized void withdraw(int amount) { System.out.println("withdrawing..."); // check if balance amount is less than withdraw // amount in this condition. deposit() synchronized // method call and deposit amount after that this // thread again execute if (this.amount < amount) { System.out.println("Amount is not enough; waiting + " for deposit..."); try { wait(); } catch (Exception e) { } } System.out.println("after deposit, balance is available: " + this.amount); this.amount -= amount; System.out.println("withdraw completed..."); System.out.println("after Withdraw, balance is available: " + this.amount); } synchronized void deposit(int amount) { System.out.println("going to deposit..."); this.amount += amount; System.out.println("deposit completed... "); notify(); } } public class Wait { public static void main(String args[]) { final PNBCustomer c = new PNBCustomer(); // create two new thread and start // them simultaneously new Thread() { @Override public void run() { c.withdraw(15000); } }.start(); new Thread() { @Override public void run() { c.deposit(10000); } }.start(); } }
输出:
withdrawing... Amount is not enough; waiting for deposit... going to deposit... deposit completed... after deposit, balance is available: 19000 withdraw completed... after Withdraw, balance is available: 4000