多线程之生产者与消费者 - 高飞网
168 人阅读

多线程之生产者与消费者

2017-07-28 02:09:46

1. synchronized实现


package com.test.thread.reentrantlock.p;

public class Res {
    private int r = 0;

    public synchronized void add() throws InterruptedException {
        if (r >= 10) {
            wait();
        }
        Thread.sleep(100);
        r = r + 1;
        System.out.println("add:" + r);
        notify();
    }

    public synchronized void cus() throws InterruptedException {
        if (r <= 0) {
            wait();
        }
        Thread.sleep(100);
        r = r - 1;
        System.out.println("            con:" + r);
        notify();
    }

    public static void main(String[] args) {
        final Res r = new Res();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        r.add();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        r.cus();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
}



2. ReentrantLock实现


package com.test.thread.reentrantlock.p;

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

public class Res2 {
    private int r = 0;
    Lock lock = new ReentrantLock();
    Condition full = lock.newCondition();
    Condition empty = lock.newCondition();
    public void add() throws InterruptedException {
        lock.lock();
        if (r >= 10) {
            full.await();
        }
        Thread.sleep(100);
        r = r + 1;
        System.out.println("add:" + r);
        empty.signal();
        lock.unlock();
    }

    public void cus() throws InterruptedException {
        lock.lock();
        if (r <= 0) {
            empty.await();
        }
        Thread.sleep(100);
        r = r - 1;
        System.out.println("            con:" + r);
        full.signal();
        lock.unlock();
    }

    public static void main(String[] args) {
        final Res2 r = new Res2();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        r.add();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        r.cus();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
}




还没有评论!
54.80.157.133