欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 多线程生产者消费者,

来源: javaer 分享于  点击 29974 次 点评:209

java 多线程生产者消费者,


 

class Res {
    private String name;
    private int count = 1;
    private boolean flag;

    public synchronized void set(String name) {
        while (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.name = name + "--" + count++;
        System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
        flag = true;
        this.notifyAll();
    }

    public synchronized void print() {
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "......消费者......" + this.name);
            flag = false;
            this.notifyAll();
        }
    }
}

class Producer implements Runnable {
    private Res r;

    public Producer(Res r) {
        this.r = r;
    }

    @Override
    public void run() {
        while (true) {
            r.set("商品");
        }
    }
}

class Consumer implements Runnable {
    private Res r;

    public Consumer(Res r) {
        this.r = r;
    }

    @Override
    public void run() {
        while (true) {
            r.print();
        }
    }
}

public class ProducerConsumerDemo {
    public static void main(String[] args) {
        Res r = new Res();
        new Thread(new Producer(r)).start();
        new Thread(new Producer(r)).start();
        new Thread(new Consumer(r)).start();
        new Thread(new Consumer(r)).start();

    }
}
出现多个生产者消费者要用while重新判断一次标记,并使用notifyAll()唤醒所有,notify可能出现只唤醒本方线程的情况,导致所有线程都等待。
缺点:每次都唤醒所有,其实只需要生产者唤醒消费者,消费者唤醒生产者就行了,JDK1.5中就实现了。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Res {
    private String name;
    private int count = 1;
    private boolean flag;
    private Lock lock = new ReentrantLock();
    private Condition condition__pro = lock.newCondition();
    private Condition condition__con = lock.newCondition();

    public void set(String name) throws InterruptedException {
        lock.lock();
        try {
            while (flag) condition__pro.await();
            this.name = name + "--" + count++;
            System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
            flag = true;
            condition__con.signal();
        } finally {
            lock.unlock();
        }
    }

    public void print() throws InterruptedException {
        lock.lock();
        try {
            while (!flag)
                condition__con.await();
                System.out.println(Thread.currentThread().getName() + "......消费者......" + this.name);
                flag = false;
                condition__pro.signal();

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

class Producer implements Runnable {
    private Res r;

    public Producer(Res r) {
        this.r = r;
    }

    @Override
    public void run() {
        while (true) {
            try {
                r.set("商品");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer implements Runnable {
    private Res r;

    public Consumer(Res r) {
        this.r = r;
    }

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

public class ProducerConsumerDemo {
    public static void main(String[] args) {
        Res r = new Res();
        new Thread(new Producer(r)).start();
        new Thread(new Producer(r)).start();
        new Thread(new Consumer(r)).start();
        new Thread(new Consumer(r)).start();

    }
}

将synchronized替换成显示的Lock操作,将Object中的wait,notify,notifyAll替换成了Condition对象,该对象可以通过Lock锁获取。

实现了本方只唤醒对方的操作。



相关文章

    暂无相关文章
相关栏目:

用户点评