Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 3rd, 2008, 06:17 AM
Registered User
 
Join Date: Aug 2003
Location: Nottingham, , United Kingdom.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Implementing a Queue Server Listener

Hi,

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 12:42 PM
Implementing a Queue Server Listener pdistant Pro Java 0 July 8th, 2008 05:06 AM
Help needed implementing shopping cart on server jon1 ASP.NET 2.0 Basics 0 November 17th, 2006 09:33 PM
XML Listener rodmcleay VB How-To 2 June 25th, 2003 08:58 PM



All times are GMT -4. The time now is 06:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.