生产者,消费者,多线程

时间:2016-11-25 08:24:37

标签: java multithreading

代码背后

所以,我的理解是如果队列(在这里共享)已满,put()方法将暂停Producer,当发生这种情况时,Consumer将正在工作并从队列中删除项目(共享),以腾出更多空间为制片人。如果put()发现队列不再满,则Producer恢复操作。此序列一直持续到Producer完成。如果队列中仍有项目,则不会使用消费者。所有消耗项目的总和应等于生产者生产的数量的总和。

问题

所以,我收到这样的错误信息。

Exception in thread "pool-1-thread-2" java.lang.NullPointerException
at Consumer.run(Consumer.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我已经阅读了有关NullPointerException的内容,我猜测的是队列设置为null,并且它指向什么都没有,所以我添加了代码来检查消费者类while(queue.poll() != null)中的null。它仍然没有解决任何问题。这是我最近7个小时的最佳猜测..任何人都可以帮我这个吗?

这是主要

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class Main {
    public static void main(String args[]){

    LinkedBlockingQueue<String> shared = new LinkedBlockingQueue<String>(1000);

    ExecutorService executor = Executors.newCachedThreadPool();
    executor.execute(new Producer(shared));
    executor.execute(new Consumer(shared,"customer1"));
    executor.execute(new Consumer(shared,"customer2"));
    executor.shutdown();

    }
}

消费者类

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Consumer implements Runnable{
    private LinkedBlockingQueue<String> queue;
    private String _name;
    private Random random = new Random();
    private int wait;
    private String cheksmax;

    public Consumer(LinkedBlockingQueue<String> shared, String name){
        queue = shared;
        _name = name;
        wait = random.nextInt(50);
    }

    @Override
    public void run() {
        int remainder = 0;

        try {
            cheksmax = queue.take().toString();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // TODO Auto-generated method stub
        while(queue.poll() != null){
            // !queue.isEmpty()
            // produce.isRunning()
            try {
                String secondQueue = queue.take().toString();
                    if(secondQueue.compareTo(cheksmax)>0){
                        cheksmax = secondQueue;
                        remainder++;
                    }
                    if(remainder % 100 == 0){
                        System.out.println(_name + ": " + remainder);
                    }
                    Thread.sleep(wait);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }       
        System.out.println(_name + " consuming Done!");
        }
    }

制作人类

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Producer implements Runnable{
    private String uuid;
    private LinkedBlockingQueue<String> queue;
    private boolean running;
    private int wait;
    private Random random = new Random();

    //constructor
    public Producer(LinkedBlockingQueue<String> shared){
        queue = shared;
        wait = random.nextInt(50);
        running = true;
    }

    public boolean isRunning(){
        return running;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        int remainder = 0;

        for(int i=0; i<5000; i++){
            try {
                uuid = UUID.randomUUID().toString();
                queue.put(uuid);
                remainder++;

                if(remainder % 100 == 0){
                    System.out.println("produced: " + remainder);
                }
                Thread.sleep(wait);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        System.out.println("producing Done!");
    }

}

HappyThanksgiving!

0 个答案:

没有答案