import java.util.*;
abstract class Worker implements Runnable {
LinkedList jobqueue = new LinkedList();
Thread thread;
int jobcount;
boolean continueWork;
String type;
public Worker(LinkedList jobqueue) {
this.jobqueue=jobqueue;
thread=new Thread(this);
thread.setDaemon(true);
jobcount=0;
continueWork=true;
thread.start();
}
public static void dis(){
int N =21; //we have 21 computers.
int j=0;
boolean [] b =new boolean[N];
boolean [] c =new boolean[N];
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
/*LiO:*/ b[i] = false;
/*Lil:*/ if (k != i){
/*Li2: begin*/ c[i] = true;
/*Li3:*/ if(b[k]){
k =i;
/*go to Lil*/ break;
}
continue;
}
else{
/*Li4: begin*/ c[i] = false;
for (j = 0;j< N ;j++){
if((j != i) && (! (c[j]))) {
/*then go to Lil*/ break;
}
continue;
}
}
/*critical section;*/
//System.out.println("b--1 "+b[i]+ "
c --1" + c[i]);
c[i] = true;
b[i] = true;
//System.out.println("b---2 "+b[i]+ "
c--- 2" + c[i]);
/*remainder of the cycle in which stopping is allowed;*/
/*go to LiO*/ break;
}
continue;
}
}
public void run() {
while(continueWork) {
dis();
if (doOneJob())
jobcount+=1;
}
System.console().format("(%s) Completed %d jobs.
",type, jobcount);
}
public void stopWorking() {
continueWork=false;
}
public abstract boolean doOneJob();
}
class Producer extends Worker {
int maxsize;
public Producer(LinkedList jobqueue, int maxsize) {
super(jobqueue);
this.maxsize=maxsize;
type="Producer";
}
public boolean doOneJob() {
if (jobqueue.size()<maxsize) {
jobqueue.addFirst(new Random().nextInt());
return true;
} else
return false;
}
}
class Consumer extends Worker {
public Consumer(LinkedList jobqueue) {
super(jobqueue);
type="Consumer";
}
public boolean doOneJob() {
if (jobqueue.size()>0) {
jobqueue.getLast();
jobqueue.removeLast();
return true;
} else
return false;
}
}
public class ProducerConsumerUnsafe {
public static void main (String[] args) {
LinkedList jobqueue=new LinkedList();
int numproducers=10;
int numconsumers=10;
final int MAXSIZE=10000000;
Producer[] producers=new Producer[numproducers];
Consumer[] consumers=new Consumer[numconsumers];
for (int i=0; i<numproducers; i=i+1)
producers[i]=new Producer(jobqueue,MAXSIZE);
for (int i=0; i<numconsumers; i=i+1)
consumers[i]=new Consumer(jobqueue);
System.console().readLine("Press ENTER to exit the program
");
try {
int sum=0;
for (int i=0; i<numproducers; i=i+1) {
producers[i].stopWorking();
producers[i].thread.join();//wait for thread to actually exit
sum+=producers[i].jobcount;
}
for (int i=0; i<numconsumers; i=i+1) {
consumers[i].stopWorking();
consumers[i].thread.join();
sum-=consumers[i].jobcount;
}
System.console().format("Expected number of jobs remaining:%d
Actually remaining in queue %d
",sum,jobqueue.size());
} catch (InterruptedException e) {
System.out.println("INTERRUPTED!");
}
}
}
I am trying to use LinkedList and i got same exception,same line.
berat@debian:~/262$ javac ProducerConsumerUnsafe.java
Note: ProducerConsumerUnsafe.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
berat@debian:~/262$ java ProducerConsumerUnsafe
Press ENTER to exit the program
Exception in thread "Thread-12" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:791)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-18" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:791)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-13" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-19" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-16" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-17" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-11" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-15" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-14" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-10" java.lang.NullPointerException
at java.util.LinkedList.remove(LinkedList.java:792)
at java.util.LinkedList.removeLast(LinkedList.java:144)
at Consumer.doOneJob(ProducerConsumerUnsafe.java:99)
at Worker.run(ProducerConsumerUnsafe.java:64)
at java.lang.Thread.run(Thread.java:619)
(Producer) Completed 72493 jobs.
(Producer) Completed 61411 jobs.
(Producer) Completed 67140 jobs.
(Producer) Completed 73825 jobs.
(Producer) Completed 72724 jobs.
(Producer) Completed 80747 jobs.
(Producer) Completed 71552 jobs.
(Producer) Completed 66773 jobs.
(Producer) Completed 67124 jobs.
(Producer) Completed 67904 jobs.
Expected number of jobs remaining:581193
Actually remaining in queue 581507
berat@debian:~/262$
i dont know how to fix it.Can you help a little bit.Add a code snippet to your website: www.paste.org