生产者消费者

生产者消费者程序的两种简单的实现,
1 sychronized wait() notifyAll()
2 Lock Condition await() signalAll()

synchronized实现:

共享资源Storage:

public class Storage {

    private static int MaxNum = 10; 

    List<Object> list = new ArrayList<Object>();

    public synchronized void pruduce(){

        System.out.println(Thread.currentThread().getId()+"号生产线程开始执行");

        while(list.size()+1>MaxNum){
            try{
                System.out.println(Thread.currentThread().getId()+"号生产线程被阻塞");
                this.wait();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        list.add(new Object());

        System.out.println(Thread.currentThread().getId()+"号生产线程执行结束");

        this.notifyAll();


    }

    public synchronized void consum(){

        System.out.println(Thread.currentThread().getId()+"号消费线程开始执行");

        while(list.size()-1<0){
            try{
                System.out.println(Thread.currentThread().getId()+"号消费线程被阻塞");
                this.wait();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        list.remove(0);

        System.out.println(Thread.currentThread().getId()+"号消费线程执行结束");

        this.notifyAll();

    }

}

生产者:

public class Producer implements Runnable {

    private Storage storage;

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

    @Override
    public void run() {
        storage.pruduce();
    }

}

消费者:

package producerConsumerSyn;

public class Consumer implements Runnable{

    private Storage storage;

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

    @Override
    public void run() {
        storage.consum();
    }

}

Lock实现:差别只有共享资源的同步方式,生产者、消费者不变

public class Storage {
    private static int maxNum = 1;

    private List<Object> list = new ArrayList<Object>();

    private final Lock lock = new ReentrantLock();

    private final Condition full = lock.newCondition();

    private final Condition empty = lock.newCondition();

    public void produce(){

        lock.lock();

        System.out.println(Thread.currentThread().getId()+"号生产线程开始执行");

        while(list.size()+1>maxNum){
            System.out.println(Thread.currentThread().getId()+"号生产线程被阻塞");
            try {
                full.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        list.add(new Object());

        System.out.println(Thread.currentThread().getId()+"号生产线程执行结束");

        empty.signalAll();

        lock.unlock();

    }

    public void consume(){

        lock.lock();

        System.out.println(Thread.currentThread().getId()+"号消费线程开始执行");

        while(list.size()-1<0){
            System.out.println(Thread.currentThread().getId()+"号消费线程被阻塞");
            System.out.println();
            try {
                empty.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        list.remove(0);

        full.signalAll();

        System.out.println(Thread.currentThread().getId()+"号消费线程执行结束");

        lock.unlock();
    }

}

你可能感兴趣的:(生产者消费者)