前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java实现生产者消费者问题

Java实现生产者消费者问题

作者头像
week
发布2018-08-24 14:23:20
4390
发布2018-08-24 14:23:20
举报
文章被收录于专栏:用户画像用户画像

实现一个队列。

队列的应用场景为:

一个生产者线程将int类型的数入列,一个消费者线程将int类型的数出列

1、Consumer.java

代码语言:javascript
复制
package com.week.pv;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 消费者
 * @author xingqijiang
 */
public class Consumer implements Runnable {

    private Queue<Integer> queue = new LinkedList<Integer>();

    public Consumer(Queue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (Thread.currentThread().isInterrupted())
                    break;
                int data;
                synchronized (queue) {
                    if (queue.size() == 0) {
                        queue.wait();
                        queue.notifyAll();
                    }
                    data = queue.poll();
                }
                System.out.println(
                        Thread.currentThread().getId() + " 消费了:" + data +  " 已出队");
                Thread.sleep(1000);
            }

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

    }

}

2、Producer.java

代码语言:javascript
复制
package com.week.pv;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;


public class Producer implements Runnable {
    private Queue<Integer> queue = new LinkedList<Integer>();
    private int length;

    public Producer(Queue<Integer> queue, int length) {
        this.queue = queue;
        this.length = length;
    }

    @Override
    public void run() {
        try {
            while (true) {

                if (Thread.currentThread().isInterrupted())
                    break;
                Random r = new Random();
                int temp = r.nextInt(100);
                System.out.println(Thread.currentThread().getId() + " 生产了:" + temp);
                synchronized (queue) {
                    if (queue.size() >= length) {
                        queue.notifyAll();
                        queue.wait();
                    } else
                        queue.add(temp);
                }
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

3、Main.java

代码语言:javascript
复制
package com.week.pv;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 主函数
 * @author xingqijiang
 *
 */
public class Main {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<Integer>();
        int length = 5;
        Producer p1 = new Producer(queue,length);
        Consumer c1 = new Consumer(queue);

        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(p1);
        service.execute(c1);

    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档