多线程编程:生产者消费者模式

1.Runnable  wait notify实现


2.堵塞队列blockingQueue


3.lock

java.util.concurrent.locks.Lock
java.util.concurrent.locks.ReentrantLock

消费者

package com.well.test.runnable;

/**
 * Created by mhc on 2017-03-09.
 */
public class Consumer implements Runnable{

    private int conNum;

    public int getConNum() {
        return conNum;
    }

    public void setConNum(int conNum) {
        this.conNum = conNum;
    }

    public Storage getTest() {
        return test;
    }

    public void setTest(Storage test) {
        this.test = test;
    }

    private Storage test;

    public Consumer(Storage test) {
        this.test = test;
    }

    @Override
    public void run() {
        try {
            int i = conNum;
            while (i > 0) {
                test.consumeCar();
                i--;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}


生产者

package com.well.test.runnable;

/**
 * Created by mhc on 2017-03-09.
 */
public class Producer implements Runnable {

    public int getProNum() {
        return proNum;
    }

    public Storage getTest() {
        return test;
    }

    public void setProNum(int proNum) {
        this.proNum = proNum;
    }

    public void setTest(Storage test) {
        this.test = test;
    }

    private int proNum;
    private Storage test;

    public Producer(Storage test) {
        this.test = test;
    }

    @Override
    public void run() {
        try {
            int i = proNum;
            while (i > 0) {
                test.produceCar();
                i--;
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


缓冲区

package com.well.test.runnable;

/**
 * Created by mhc on 2017-03-09.
 */
public class Storage {

    public int num = 0;

    public synchronized void produceCar() throws InterruptedException {

        if (num >= 10) {
            this.wait();
        }
        Thread.sleep(50);
        num++;
        System.out.println(Thread.currentThread().getName() + "生产了一辆小车,当前" + num);
        this.notify();
    }

    public synchronized void consumeCar() throws InterruptedException {

        if (num <= 0) {
            this.wait();
        }
        Thread.sleep(30);
        num--;
        System.out.println(Thread.currentThread().getName() + "消费了一辆小车,当前" + num);
        this.notify();
    }

}


package com.well.test.queue;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by mhc on 2017-03-09.
 */
public class Storage {

    public BlockingQueue blockingQueue = new LinkedBlockingDeque();

    public  void produceCar() throws InterruptedException {

        Thread.sleep(50);

        Random random = new Random();
        int num = random.nextInt(10000);

        blockingQueue.put(num);

        System.out.println(Thread.currentThread().getName() + "生产了一辆小车,当前" + num);
    }

    public  void consumeCar() throws InterruptedException {


        Thread.sleep(30);

        int num = (int) blockingQueue.take();

        System.out.println(Thread.currentThread().getName() + "消费了一辆小车,当前" + num);

    }

}

package com.well.test.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by mhc on 2017-03-09.
 */
public class Storage {

    public int num = 0;

    final Lock lock = new ReentrantLock();

    public void produceCar() throws InterruptedException {

        lock.lock();
        try {
            Thread.sleep(50);
            num++;
            System.out.println(Thread.currentThread().getName() + "生产了一辆小车,当前" + num);
        } finally {
            lock.unlock();
        }
    }

    public void consumeCar() throws InterruptedException {

        lock.lock();
        try {
            while (num <= 0){
                Thread.sleep(100);
            }
            Thread.sleep(30);
            System.out.println(Thread.currentThread().getName() + "消费了一辆小车,当前" + num);
            num--;
        } finally {
            lock.unlock();
        }

    }

}

package com.well.test.runnable;

/**
 * Created by mhc on 2017-03-09.
 */
public class Test {

    public static void main(String args[]) {

        Storage storage = new Storage();
        Producer productor1 = new Producer(storage);
        productor1.setProNum(16);
        Producer productor2 = new Producer(storage);
        productor2.setProNum(14);

        Consumer consumer1 = new Consumer(storage);
        consumer1.setConNum(9);
        Consumer consumer2 = new Consumer(storage);
        consumer2.setConNum(6);

        Thread thread_pro1 = new Thread(productor1);
        Thread thread_pro2 = new Thread(productor2);

        Thread thread_con1 = new Thread(consumer1);
        Thread thread_con2 = new Thread(consumer2);

        thread_pro1.start();
        thread_pro2.start();

        thread_con1.start();
        thread_con2.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值