Multi-threaded code demonstration

Multi-threaded code demonstration

wait/notify demo

/**
 * 演示wait()/notify()
 */
public class WaitAndNotifyTest {
    
    

    public static void main(String[] args) {
    
    
        Object obj= new Object();
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                      synchronized (obj){
    
    
                          try {
    
    
                              System.out.println("等待前代码");
                              obj.wait();  //线程等待
                              System.out.println("等待后代码");
                          } catch (InterruptedException e) {
    
    
                              e.printStackTrace();
                          }
                      }
            }
        }).start();

        try {
    
    
            Thread.sleep(2000);  //保证等待线程执行
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                synchronized (obj){
    
    
                        obj.notify();//唤醒等待线程
                    System.out.println("唤醒后代码");
                }
            }
        }).start();
    }
}

Insert picture description here

Executing the above code can draw conclusions:
1. wait()/notify() must use the same lock object, and call in the synchronized code block
2. The thread will not wake up the thread immediately after the execution of notify(), and only after the current thread is executed Will wake up and release the lock object

interrupt interrupt wait to release the lock

/**
 * 演示interrupt中断wait释放锁
 */
public class WaitAndNotifyTest {
    
    
    public static void main(String[] args) {
    
    
        Object obj= new Object();
        Thread thread = new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                synchronized (obj) {
    
    
                    try {
    
    
                        System.out.println("等待前代码");
                        obj.wait();  //线程等待
                        System.out.println("等待后代码");
                    } catch (InterruptedException e) {
    
    
                        System.out.println("报错");
                    }
                }
            }
        });
        thread.start();
      try {
    
    
            Thread.sleep(2000);  //保证线程等待线程执行
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        thread.interrupt();
    }
}

One production one consumption

public class ValueOp {
    
    
    private String value="";

    public void setValue(String val){
    
    
        synchronized (this){
    
    
            if(!value.equals("")){
    
    //value有值等待
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
            value=val;
            System.out.println("set设置的值是"+value);
            this.notify();
        }
    }
    public void getValue( ){
    
    
        synchronized (this){
    
    
            if(value.equals("")){
    
    //value没值等待
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
            System.out.println(value);
            value="";
            this.notify();
        }
    }
}
public class ProducerThread extends Thread {
    
    
    ValueOp value;
    public ProducerThread(ValueOp val) {
    
    
        value=val;
    }

    @Override
    public void run() {
    
    
        while (true){
    
    
            value.setValue(System.currentTimeMillis()+"---->"+System.nanoTime());
        }
    }
}
public class ConsumerThread extends Thread {
    
    
    ValueOp value;
    public ConsumerThread(ValueOp val) {
    
    
        value=val;
    }
    @Override
    public void run() {
    
    
        while (true){
    
    
            value.getValue();
        }
    }
}

public class ProducerConsumerTest {
    
    
    public static void main(String[] args) {
    
    
        ValueOp valueOp = new ValueOp();
        ProducerThread producerThread = new ProducerThread(valueOp);
        ConsumerThread consumerThread = new ConsumerThread(valueOp);
        producerThread.start();
        consumerThread.start();
    }
}

Produce more and consume more

/**
 * Created by Administrator on 2020-12-01.
 */
public class ValueOp {
    
    
    private String value="";

    public void setValue(String val){
    
    
        synchronized (this){
    
    
            while(!value.equals("")){
    
    //value有值等待
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
            value=val;
            System.out.println("set设置的值是"+value);
            this.notifyAll();
        }
    }
    public void getValue( ){
    
    
        synchronized (this){
    
    
            while (value.equals("")){
    
    //value没值等待,该线程如果被唤醒必须再进行判断,防止已经被其他线程消费
                try {
    
    
                    this.wait();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
            System.out.println("get得到的值是"+value);
            value="";
            this.notifyAll();
        }
    }
}
public class ProducerThread extends Thread {
    
    
    ValueOp value;
    public ProducerThread(ValueOp val) {
    
    
        value=val;
    }

    @Override
    public void run() {
    
    
        while (true){
    
    
            value.setValue(System.currentTimeMillis()+"---->"+System.nanoTime());
        }
    }
}

public class ConsumerThread extends Thread {
    
    
    ValueOp value;
    public ConsumerThread(ValueOp val) {
    
    
        value=val;
    }

    @Override
    public void run() {
    
    
        while (true){
    
    
            value.getValue();
        }
    }
}
public class ProducerConsumerTest {
    
    
    public static void main(String[] args) {
    
    
        ValueOp valueOp = new ValueOp();
        ProducerThread producerThread = new ProducerThread(valueOp);
        ProducerThread producerThread2 = new ProducerThread(valueOp);
        ProducerThread producerThread3 = new ProducerThread(valueOp);
        ConsumerThread consumerThread = new ConsumerThread(valueOp);
        ConsumerThread consumerThread2 = new ConsumerThread(valueOp);
        ConsumerThread consumerThread3 = new ConsumerThread(valueOp);
        producerThread.start();
        producerThread2.start();
        producerThread3.start();
        consumerThread.start();
        consumerThread2.start();
        consumerThread3.start();
    }
}

Guess you like

Origin blog.csdn.net/weixin_45742032/article/details/110390413