java multithreading, communication between two threads

Example: two threads execute in turn package com.heima.day25; public class Demo004 { public static void main(String[] args) { Print print = new Print...

Example: two threads execute in turn

package com.heima.day25; public class Demo004 { public static void main(String[] args) { Print print = new Print(); new Thread(){ public void run() { for(int i=1;i<=100;i++){ try { print.print1(); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); new Thread(){ public void run() { for(int i=1;i<=100;i++){ try { print.print2(); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); } } class Print{ private int flag = 1; /* * Lock object is obj * If the wait() and notify() methods are not called by the lock object, an error will be reported * java.lang.IllegalMonitorStateException */ Object obj = new Object(); public void print1() throws InterruptedException{ synchronized (obj) { if(flag!=1){ obj.wait(); } System.out.print("black"); System.out.print("Horse"); System.out.print("Cheng"); System.out.print("order"); System.out.print("member"); System.out.print("\r\n"); flag = 2; obj.notify(); System.out.println(Thread.currentThread().getName()); } } public void print2() throws InterruptedException{ synchronized (obj) { if(flag != 2){ obj.wait(); } System.out.print("pass"); System.out.print("wisdom"); System.out.print("Sowing"); System.out.print("passenger"); System.out.print("\r\n"); flag = 1; obj.notify(); System.out.println(Thread.currentThread().getName()); } } }

wait(): causes the current thread to wait and enter a wait blocked state. Until another thread calls the notify() or notifyAll() method of the synchronization lock object to wake up the thread.

notify(): wake up a single thread waiting on the synchronization lock object. If there are multiple threads waiting on the synchronization lock object, one of them will be randomly selected for wake-up operation. Only when the current thread abandons the lock on the synchronization lock object, can the awakened thread be executed.

Note: wait() and notify() must be executed by the lock object, otherwise an error will be reported: java.lang.IllegalMonitorStateException.

Some information:

The fourth edition of java programming thought describes: "wait(), notify(), notifyAll() methods of thread operation can only be called in synchronization control method or synchronization control block. If it is called in an asynchronous control method or control block, the program can be compiled, but when it runs, it will get an IllegalMonitorStateException exception, accompanied by some ambiguous information, such as "the current thread is not the owner". In fact, the meaning of exception is that the task calling wait(), notify(), notifyAll() must 'own' (get) the lock of the object before calling these methods. "

3 December 2019, 15:15 | Views: 1910

Add new comment

For adding a comment, please log in
or create account

0 comments