java多线程之间通讯

 

  • Wait的作用
  1. 是让当前线程从运行状态到休眠状态
  • notify的作用
  1. 是让当前线程从休眠状态到运行状态
  • 如何停止线程
  1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止
  2.  使用stop方法强行终止线程(不推荐使用,因为stopsuspendresume一样,也可能发生不可预料的结果)。
  3. 使用interrupt方法中断线程。
  • interrupt的作用
  1. 当对阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出InterruptException异常,停止线程
  • 线程之间的通讯
  1. 使用synchronized、notify()、wait()、notifyAll()进行线程之间的通讯
  • Sleep()和wait()的区别
  1. Sleep()不会释放锁的资源、wait()会释放锁的资源
  2. Sleep()是时间到了就会被唤醒
  3. Wati()可以又notify()唤醒
  • Lock锁和synchronized同步锁的区别
  1. Lock锁属于手动控制                      灵活性好
  2. Synchronized同步锁属于自动控制  不利于扩展
  • Lock锁的方法
  1. public Lock lock = new ReentrantLock();    //  创建锁对象  多个线程锁对象需一致
  2. Condition condition = lock.newCondition(); // 创建 condition对象   多个线程condition对象需一致
  3. Lock.lock();   // 上锁
  4. condition.await() ;  //等待
  5. condition.signal() ;  // 唤醒
  6. Lock.unlock();      // 释放锁资源
  1. import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * 多线程之间通讯
     * Create by wangxb
     * 2019-05-14 21:38
     */
    // 水果
    class Fruit{
        public String name;
        public String color;
        public boolean flag = false;
        public Lock lock = new ReentrantLock();  // 创建锁对象  多个线程的锁对象需一致
    }
    // 生产者
    class  Producer implements Runnable{
    
        private Fruit fruit;
        public Condition condition;
        public Producer(Fruit fruit, Condition condition){
            this.fruit = fruit;
            this.condition = condition;
        }
        @Override
        public void run() {
            int i = 0;
            while (true){
                fruit.lock.lock();  // 开始上锁
                    if (fruit.flag){
                        try {
                            condition.await();  // 等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else{
                        if (i == 0){
                            fruit.name = "香蕉";
                            fruit.color = "黄色";
                        }else{
                            fruit.name = "辣椒";
                            fruit.color = "红色";
                        }
                        i = (i+1)%2;
                        fruit.flag = true;
                        System.out.println("生产者生产了  "+fruit.color+" 的 "+fruit.name+"---"+i);
                        condition.signal();  // 唤醒另外线程
                        fruit.lock.unlock();  // 释放锁
                    }
                }
    
            }
        }
    // 消费者
    class Consumer implements Runnable {
        private Fruit fruit;
        public Condition condition;
        public Consumer(Fruit fruit, Condition condition){
            this.fruit = fruit;
            this.condition = condition;
        }
        @Override
        public void run() {
            while (true){
                fruit.lock.lock();
                if (!fruit.flag) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    System.out.println("消费者拿走了  "+fruit.color+" 的 "+fruit.name);
                    fruit.flag = false;
                }
                condition.signal();
                fruit.lock.unlock();
    
            }
        }
    }
    public class Test {
    
        public static void main(String[] args) {
            Fruit fruit = new Fruit();
            Condition condition = fruit.lock.newCondition();
            Producer producer = new Producer(fruit, condition);
            Consumer consumer = new Consumer(fruit, condition);
            Thread t1 = new Thread(producer);
            Thread t2 = new Thread(consumer);
            t1.start();
            t2.start();
        }
    }

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值