Behind the code
So, my understanding is that the put() method suspends the Producer if the queue(which is shared here) is full, when this happens Consumer will be working and remove items from the queue(shared), to make more room for the Producer. The Producer resumes operation if put() has found out the queue is no longer full. This sequence continues until the Producer finishes. Consumer is not done if there still item left in queue. The sum of all Consumed item should equal to that of the amount Producer produced.
Question
So, I'm getting error message like this.
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)
I have read about NullPointerException and what i am guessing is that queue is set as null, and it is pointing at nothing, so I added the code to check for a null in consumer class while(queue.poll() != null)
. It still solves nothing. This was my best guess for last 7 hours.. Can anybody help me with this?
This is Main
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();
}
}
Consumer class
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!");
}
}
Producer class
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!