多线程代码演示

多线程代码演示

wait/notify演示

/**
 * 演示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();
    }
}

在这里插入图片描述

执行上面代码可以得出结论:
1.wait()/notify()必须使用同一锁对象,并且在同步代码块中调用
2.执行notify()后线程不会立即唤醒线程,当前线程执行完后才会唤醒并且释放锁对象

interrupt中断wait释放锁

/**
 * 演示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();
    }
}

一生产一消费

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();
    }
}

多生产多消费

/**
 * 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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值