Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
|
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 8th, 2008, 05:06 AM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
broken pipe error in implementing client -server octavius BOOK Beginning Linux Programming, 3rd Edition 1 December 17th, 2011 01:42 PM
Implementing a Queue Server Listener pdistant Java Basics 0 July 3rd, 2008 06:17 AM
Help needed implementing shopping cart on server jon1 ASP.NET 2.0 Basics 0 November 17th, 2006 10:33 PM
XML Listener rodmcleay VB How-To 2 June 25th, 2003 08:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.