The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Thread use in servlets...

Hi,

I am working on a servlet which starts around 7 threads, that fetch one web page each for scraping. Now, since the main thread of the servlet exits after spinning off the threads, the servlet just ends without waiting for the threads to complete their operations.

To tackle that, I have made the main thread to join on the last thread created. This approach is working fine, but I don't feel right about this.

Is there a better way to implement this ?

Any suggestions will be appreciated a lot.
tss
Monday, June 12, 2006
 
 
It will work only if you can guarantee that the last thread created will be the last one to finish its work, which is unlikely.
Keep a count of running threads, and wait until it hits zero.  You can either do this explicitly, or in a semaphore class.
bmm6o Send private email
Monday, June 12, 2006
 
 
Use an Actor approach. Look at SEDA for an example. You have threads blocked on message queues and you pass work between queues.
son of parnas
Monday, June 12, 2006
 
 
If you don't like join, then use the listener pattern?

interface WorkerListener {
  void onFinished();
}

abstract class Worker extends Thread {
  private List<WorkerListener> listeners;

  protected abstract void doWork();
 
  public void addListener(WorkerListener listener) {
    listeners.add(listener);
  }

  public void removeListener(WokerListener listener) {
    listeners.remove(listener);
  }

  private void fireFinishedWork() {
      for(Iterator<WorkerListener> iter = listeners.iterator(); iter.hasNext(); ) {
          iter.next().onFinished();
      }
  }

  public void run() {
      doWork();
      fireFinishedWork();
  }
}

class MyServlet extends HttpServlet implements WorkerListener {

    private int workerCount;

    public void onFinished() {
        synchronized(this) {
            workerCount--;
        }
    }

    private void myService(Worker[] workers) {
        for(int i = 0; i < workers.length; i++) {
          synchronized(this) {
            workers[i].start();
            workerCount++;
          }
      }

      // implement some controlled looping here instead
      while(true) {
          synchronized(this) {
              if(workerCount == 0) break;
              try {
                  wait(timeout);
              } catch(InterruptedException e) {
                  ;
              }
          }
      }
  }

}
Dino Send private email
Monday, June 12, 2006
 
 
I've been goofing around with actors lately, so I wanted to see what SEDA was.  Googling "SEDA" or "SEDA Actor" isn't doing much good.  Could you provide more details or a link?

Thanks.

Wednesday, June 14, 2006
 
 
If you use Java 5 there's some really cool ThreadPool classes that are now part of the JRE.  Check out the java.utils.concurrency package.  There is at least one ThreadPool in there that I believe does the thread syncing you need to wait until all the worker threads are done.
Paul Norrie Send private email
Thursday, June 15, 2006
 
 
Why not loop through all your threads and join to each one of them in turn?
Mike Weller
Friday, June 16, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz