生产者消费者

synchronized实现

public class TestProducerAndConsumer {
    public static void main(String[] args) {
        Resource resource = new Resource();
        ProducerThread p1 = new ProducerThread(resource);
        ConsumerThread c1 = new ConsumerThread(resource);
        p1.start();
        c1.start();
    }
}

class Resource {
    //当前资源数量
    private int num = 0;
    //资源池中允许存放的资源数目
    private int size = 1;
    //从资源池中取走资源
    public synchronized void remove() {
        if (num > 0) {
            System.out.println("消费者" + Thread.currentThread().getName() + "消耗一件资源," + "当前线程池有" + --num + "个");
            this.notifyAll();//通知生产者生产资源
        } else {
            try {
                //如果没有资源,则消费者进入等待状态
                this.wait();
                System.out.println("消费者" + Thread.currentThread().getName() + "线程进入等待状态");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //向资源池中添加资源
    public synchronized void add() {
        if (num < size) {
            System.out.println(Thread.currentThread().getName() + "生产一件资源,当前资源池有" + ++num + "个");
            //通知等待的消费者
            this.notifyAll();
        } else {
            //如果当前资源池中有10件资源
            try {
                this.wait();//生产者进入等待状态,并释放锁
                System.out.println(Thread.currentThread().getName() + "线程进入等待");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class ConsumerThread extends Thread {
    private Resource resource;
    public ConsumerThread(Resource resource) {
        this.resource = resource;
    }
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.remove();
        }
    }
}
class ProducerThread extends Thread {
    private Resource resource;
    public ProducerThread(Resource resource) {
        this.resource = resource;
    }
    @Override
    public void run() {
        //不断地生产资源
        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.add();
        }
    }
}

lock & Condition结合

class Resource {
    //当前资源数量
    private int num = 0;
    //资源池中允许存放的资源数目
    private int size = 1;
    private Lock lock = new ReentrantLock();
    //condition对象是依赖于lock对象的,意思就是说condition对象需要通过lock对象进行创建出来
    private Condition condition = lock.newCondition();
    //从资源池中取走资源
    public void remove() {
        lock.lock();
        try {
            if (num > 0) {
                System.out.println("消费者" + Thread.currentThread().getName() + "消耗一件资源," + "当前线程池有" + --num + "个");
                condition.signalAll();//通知生产者生产资源
            } else {
                try {
                    //如果没有资源,则消费者进入等待状态
                    condition.await();
                    System.out.println("消费者" + Thread.currentThread().getName() + "线程进入等待状态");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            lock.unlock();
        }
    }
    //向资源池中添加资源
    public void add() {
        lock.lock();
        try {
            if (num < size) {
                System.out.println(Thread.currentThread().getName() + "生产一件资源,当前资源池有" + ++num + "个");
                //通知等待的消费者
                condition.signalAll();
            } else {
                //如果当前资源池中有10件资源
                try {
                    condition.await();//生产者进入等待状态,并释放锁
                    System.out.println(Thread.currentThread().getName() + "线程进入等待");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            lock.unlock();
        }
    }
}
class ConsumerThread extends Thread {
    private Resource resource;

    public ConsumerThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.remove();
        }
    }
}
class ProducerThread extends Thread {
    private Resource resource;

    public ProducerThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        //不断地生产资源
        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.add();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值