Example of thread communication: use two threads to print 1-100. Thread 1, 2 print alternately

An example of thread communication: use two threads to print 1-100. Thread 1, 2 alternately print
the three methods involved in
wait Once this method is executed, the current thread enters the blocking state and releases the synchronization monitor.
Once notify is executed, this method will wake up a thread that is being
waited for . If multiple threads are waited , the thread with a higher priority will be awakened. Notifyall will wake up all the waited threads once this method is executed.
Points to note:
1. The call of the wait() notify() notifyall() method can only be used in synchronous code blocks or synchronous methods.
2.wait() notify() notifyall() The caller of the three methods must be a synchronous monitor in a synchronous code block or synchronous method, otherwise illegalMonitorStateException will occur
3.wait() notify() notifyall() is defined in lava.long.Object class

//线程通信的例子:使用两个线程打印1-100.线程1,2交替打印
class Number implements Runnable{
    
    
    private static int number = 1;
    @Override
    public void run() {
    
    
        while (true){
    
    
            synchronized (this) {
    
    
                notify();
                if (number <= 100) {
    
    
                    try {
    
    
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;
                    //使得调用如下wait()方法的线程进入阻塞状态
                    try {
    
    
                        wait();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }

                }else{
    
    
                    break;
                }
            }
        }
    }

public static class why {
    
    
    public static void main(String[] args) {
    
    
        Number number = new Number();
        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);
        t1.start();
        t2.start();
        t1.setName("线程1");
        t2.setName("线程2");

    }
}
}

Interview questions:

Similarities and differences between Sleep and wait methods
1. Similarities : Once the method is executed, the current thread can be in a blocked state;
2. Differences: (1) The two methods are declared in different positions: sleep is declared in the Thread class and sleep is declared in the object class wait; (2) The calling requirements are different: sleep can be called in any required scene; wait must be used in a synchronized code block or a synchronized method; (3) Regarding whether to release the synchronization monitor: both methods are in a synchronized code block or In the synchronous method, sleep will not release the lock; wait will release the lock.

Guess you like

Origin blog.csdn.net/HenryFanQAQ/article/details/110728818