码迷,mamicode.com
首页 > 编程语言 > 详细

线程间通信 生产者、消费者虚假唤醒

时间:2020-03-14 20:31:47      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:print   --   tar   shared   cep   data   唤醒   share   ant   

线程通信中要预防虚假唤醒  注意(在哪里停就在哪里启动)

class shareDataOne{
    private int number = 0;
    public synchronized void incr() throws InterruptedException {
        if(number != 0){
            this.wait();//等待  不可以用if 必须用while进行判断
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"\t"+number);

        this.notifyAll();//唤醒
    }

    public synchronized void decr() throws InterruptedException {
        if(number == 0){
            this.wait();//等待 不可以用if 必须用while进行判断
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"\t"+number);

        this.notifyAll();
    }
}
结果

DD 0
AA 1
CC 2
DD 1
DD 0
CC 1

 
class shareDataOne{
    private int number = 0;
    public synchronized void incr() throws InterruptedException {
        while(number != 0){ //正确的写法
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"\t"+number);

        this.notifyAll();
    }

    public synchronized void decr() throws InterruptedException {
        while(number == 0){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"\t"+number);

        this.notifyAll();
    }
}

结果:

DD 0
CC 1
DD 0
AA 1
DD 0
CC 1

 

 

使用Lock实现

public class Test02 {

    public static void main(String[] args) {
        shareDataOne shareDataOne = new shareDataOne();
        new Thread(() -> {
            for (int i = 0; i <= 10; i++) {
                shareDataOne.incr();
            }
        }, "AA").start();
        new Thread(() -> {
            for (int i = 0; i <= 10; i++) {
                shareDataOne.decr();
            }
        }, "BB").start();
        new Thread(() -> {
            for (int i = 0; i <= 10; i++) {
                shareDataOne.incr();
            }
        }, "CC").start();
        new Thread(() -> {
            for (int i = 0; i <= 10; i++) {
                shareDataOne.decr();
            }
        }, "DD").start();
    }


}
class shareDataOne {
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition cd = lock.newCondition();

    public void incr() {
        lock.lock();
        try {
            while (number != 0) {
                cd.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "\t" + number);
            cd.signalAll();//此处唤醒用ALL避免死锁
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }

    }

    public void decr() {
        lock.lock();
        try {
            while (number == 0) {
                cd.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "\t" + number);

            cd.signalAll();//此处唤醒用ALL避免死锁
        } catch (Exception e) {

        } finally {
            lock.unlock();
        }
    }
}

 

线程间通信 生产者、消费者虚假唤醒

标签:print   --   tar   shared   cep   data   唤醒   share   ant   

原文地址:https://www.cnblogs.com/hpdblogs/p/12493624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!