Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Advanced-java] Manager - Worker thread question


  • From: ejfried@xxxxxxxxxxxxxxxxxxxxx (friedman_hill ernest j)
  • Subject: [Advanced-java] Manager - Worker thread question
  • Date: Mon, 14 Jan 2002 10:00:04 -0800 (PST)

I think Carl Swanson wrote:
> 
> 
> I need to set up one thread, a manager thread, that sets up
> sub threads with worker classes that do work, with IDs attached to each one.
> 
> Then, when work comes in for a certain ID, the manager finds the one
> it wants, then the work is called against the right thread. Many calls
> need to com in and work simultaneously.
> 
> (Kind of like a manager calling an employee when work
> for them comes in)
> 
> I can spawn worker threads, but then how do I get them to wait
> until called, and then call the correct one? Wait/notify doesn't work 
> that way
> as far as I can tell.
> 
> Do I simpy call a method on the object in the different thread?
> Do I set the worker thread to wait and then the call wakes it up?

Indeed, this is the basic outline. The threads look something like
this (I left out exception handling)

class MyThread implements Runnable {

  private List m_work = new LinkedList();

  public void run() {
    while (true) {
      WorkUnit work = null;
      synchronized (this) {
        while (m_work.size() == 0)
           wait();
        work = m_work.removeLast();
      }
      doWork(work);
    }
  }

  public synchronized void assignWork(WorkUnit work) {
      m_work.addFirst(work);
      notify();
  }  

  private void doWork(WorkUnit work) {
    // Do whatever.
  }

}



}






> 
> Any tips would be much appreciated.
> 
> Thanks,
> 
> Carl
> cswanson@xxxxxxxxxx
> 
> 
> 
> _______________________________________________
> Advanced-java mailing list
> Advanced-java@xxxxxxxxxxxxxxxxxxxxxx
> http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java
> 



---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
Org. 8920, MS 9012                  ejfried@xxxxxxxxxxxxx
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550