美文网首页JavaJava学习笔记
消费者生产者线程并发

消费者生产者线程并发

作者: firststep | 来源:发表于2019-05-23 14:29 被阅读0次

问题

生产者消费者模式,synchronized锁住一个LinkedList,一个生产者,只要队列不满,生产后往里放,一个消费者只要队列不空,向外取,两者通过wait()和notify()进行协调。

代码

package queueStudy;

import java.util.ArrayList;
import java.util.List;

public class QueueTest {
    public static Object singal = new Object();
    boolean bFull = false;
    private List list = new ArrayList();

    public void product(String arg) {
        synchronized (singal) {
            if (list.size()<5) { // !bFull
                bFull = true;
                System.out.println("product"+arg);
                list.add(arg);
                singal.notify();
            } else {
                try {
                    singal.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(arg);
        }
    }

    public String consume() {
        synchronized (singal) {
            if (list.size()<=0) { //!bFull
                try {
                    singal.wait();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            bFull = false;
            System.out.println("consume");
            singal.notify();
        }
        String result = "";
        if (list.size() > 0) {
            result = list.get(list.size() - 1).toString();
            list.remove(list.size() - 1);
        }
        return result;

    }
}

package queueStudy;

public class TestConsumer implements Runnable {
    QueueTest queue;

    public TestConsumer() {
        // TODO Auto-generated constructor stub
    }

    public TestConsumer(QueueTest obj) {
        this.queue = obj;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                queue.consume();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
package queueStudy;

public class TestProduct implements Runnable {
    QueueTest t;

    public TestProduct() {

    }

    public TestProduct(QueueTest t) {
        this.t = t;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                t.product("test" + i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
package queueStudy;

public class TestQueue {
    //@Test
    public void test() {
        QueueTest queue = new QueueTest();
        Thread customer = new Thread(new TestConsumer(queue));
        Thread product = new Thread(new TestProduct(queue));
        customer.start();
        product.start();
    }

    public static void main(String[] args) {
        TestQueue testQueue = new TestQueue();
        testQueue.test();
    }
}

结果显示

image.png

解释

  1. 在队列中如果是空的,消费者就等待,等待队列中有资源的的时候消费者可以继续使用。
  2. 如果队列大于5个,说明队列满了。生产者等待。等队列少于五个继续生产添加。

相关文章

网友评论

    本文标题:消费者生产者线程并发

    本文链接:https://www.haomeiwen.com/subject/yqgvzqtx.html