Implementing a Queue Server Listener
Hi,
Added this to 'Beginning Java' but no replies:
Iâm implementing a queue Server using a Stack which has class instances as items within the queue. Each object item implements the Runnable interface and run's a specific task. The problem that Iâm facing is if I add âobject 1, 2, 3 and 4â to the queue (in that order)
(queue becomes)
Element 0 = object 1 (running)
Element 1 = object 2 (running)
Element 2 = object 3 (running)
Element 3 = object 4 (running)
and object 2 and 3 finish whist 1 and 4 are still running
(queue is)
Element 0 = object 1 (running)
Element 1 = object 2 (stopped running)
Element 2 = object 3 (stopped running)
Element 3 = object 4 (running)
then I need objects 2 and 3 to fire an event to the Queue Server to tell it to remove objects 2 and 3.
(queue should now become)
Element 0 = object 1 (running)
Element 1 = object 4 (running)
Iâm not sure if the queue server will need more than one thread and needs to wait();
See below snippet code (note: RunCommandGUI could be the objectâs 1,2,3 or 4)
public class QueueServerThread implements Runnable {
protected Thread objrunner = null;
protected static Stack <RunCommandGUI>queue;
@Override
synchronized public void run() {
Thread thisThread = Thread.currentThread();
System.out.println("run executed");
this.pushOntoQueue();
while (this.objrunner == thisThread) {
try {
System.out.println("Queue server asleep ");
this.runsomeChecks(...); //.....
Thread.sleep(5000);
System.out.println("Queue server now awake\n");
}
catch (InterruptedException ie) {
ie.printStackTrace();
return;
}
catch (NullPointerException ne) {
ne.printStackTrace();
}
catch (Exception ie) {
ie.printStackTrace();
return;
}
}
System.out.println("Queue server Stoppedâ);
}
protected boolean pushOntoQueue () {
RunCommandGUI obj_rungui = null;
try {
for (int i = 0; i < 5; i++) {
obj_rungui = new RunCommandGUI(âobject â + (i+1) );
obj_rungui.start();
synchronized (this.queue) {
this.queue.push(obj_rungui);
this.queue.notifyAll();
}
}
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
public class RunCommandGUI extends javax.swing.JFrame implements Runnable {
Thread objrunner = null;
public void start() {
if (this.objrunner ==null) {
this.objrunner = new Thread(this);
this.objrunner.start();
System.out.println("....Starting thread: " + this.objrunner.getName());
}
}
public void run() {
Thread thisThread = Thread.currentThread();
while (this.objrunner == thisThread) {
try {
System.out.println("Asleep for XX seconds\n");
this.objrunner.sleep(10000);
System.out.println("thread now awake\n",true);
if (this.performATask()) {
this.objrunner = null;
// Some how tell Queue Server that this objectâs thread has now been terminated so take it out of the queue.
â¦.
}
}
catch (InterruptedException ie) {
System.out.println("InterruptedException: " + ie.getMessage().toString());
return;
}
}
}
private Boolean performATask() {
â¦â¦
}
}
Can anyone help or advise? Iâm currently running Windows XP pro with java 1.6
Thanks,
P
|