Kan nogle se fejlen i dette program med (Thread)
Når jeg kører dette program får jeg følgende :Thread-0 insætter 890 i queue
mypackage3.QueueException: QueueException on dequeue: Queue empty
at mypackage3.QueueArrayBased.dequeue(QueueArrayBased.java:70)
at mypackage3.Consumer.run(Consumer.java:14)
at java.lang.Thread.run(Thread.java:534)
Thread-1 insætter 2277 i queue
Thread-2 insætter 2444 i queue
Hvorfor er min queue empty, når det tydeligvis fremgår at den først sætter ind i queuen, før exceptioenen kommer...
Mine klasser jeg arbejder med er:
public class ThreadsApp
{
QueueArrayBased queue = QueueArrayBased.getInstance();
Thread prod1, prod2, prod3;
Thread cons1, cons2, cons3;
public ThreadsApp()
{
prod1 = new Thread(new Producer(queue));
prod2 = new Thread(new Producer(queue));
prod3 = new Thread(new Producer(queue));
cons1 = new Thread(new Consumer(queue));
//cons2 = new Consumer(queue);
//cons3 = new Consumer(queue);
//prod1.setPriority(7);
prod1.start();
prod2.start();
prod3.start();
//cons1.setPriority(2);
cons1.start();
//cons2.start();
//cons3.start();
}
public static void main(String[] args)
{
ThreadsApp threadsApp = new ThreadsApp();
}
}
.......................................................
public class Producer implements Runnable
{
QueueArrayBased queue;
TestObjekter tob;
public Producer(QueueArrayBased queue)
{
this.queue = queue;
}
public void run()
{
int random = (int)(Math.random()*3000);
tob = new TestObjekter(random);
queue.enqueue(tob);
System.out.println(Thread.currentThread().getName()+" insætter "+tob.getTal()+" i queue");
}
}
.............................................................
public class Consumer implements Runnable
{
QueueArrayBased queue;
TestObjekter tob;
public Consumer(QueueArrayBased queue)
{
this.queue = queue;
}
public void run()
{
tob = (TestObjekter)queue.dequeue();
System.out.println(Thread.currentThread().getName()+" henter "+tob.getTal()+" fra queue");
}
}
...........................................................
public class QueueArrayBased implements QueueInterface
{
private int MAX_QUEUE = 4; // maximum size of queue
private Object[] items;
private int front, back, count;
private final static QueueArrayBased queue = new QueueArrayBased();
public QueueArrayBased() {
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end default constructor
// queue operations:
public boolean isEmpty() {
return count == 0;
} // end isEmpty
public boolean isFull() {
return count == MAX_QUEUE;
} // end isFull
public synchronized void enqueue(Object newItem) {
if (!isFull())
{
back = (back+1) % (MAX_QUEUE);
items[back] = newItem;
++count;
notifyAll();
}
else
{
try
{
wait();
}
catch (InterruptedException e)
{
System.out.println(e);
}
throw new QueueException("QueueException on enqueue: "
+ "Queue full");
} // end if
} // end enqueue
public synchronized Object dequeue() throws QueueException {
try
{
if (!isEmpty())
{
// queue is not empty; remove front
Object queueFront = items[front];
front = (front+1) % (MAX_QUEUE);
--count;
notifyAll();
return queueFront;
}
else
{
wait();
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
throw new QueueException("QueueException on dequeue: "+"Queue empty");
}
public void dequeueAll()
{
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end dequeueAll
public Object peek() throws QueueException {
if (!isEmpty()) {
// queue is not empty; retrieve front
return items[front];
}
else {
throw new QueueException("Queue exception on peek: " +
"Queue empty");
} // end if
} // end peek
public static synchronized QueueArrayBased getInstance()
{
return queue;
}
} // end QueueArrayBased
