Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread design  RSS feed

 
Chad Patrick
Greenhorn
Posts: 11
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a server program that has lets say 2 threads initially.. one thread is the main thread that accepts the connections (spins off connections in new client threads). the other thread(call it auxthread) is a thread thats called to do work when called by one of the clients.
How do you design it so that auxthread gets created but goes into an idle state.. so say client1 can call auxthread.dosomething(some var) and have it done in the auxthread while client 1 continutes. If another client logs in it should be able to call the same auxthread.dosomething(some var) function in the worker thread..And when they are done, auxthread goes back idle
Im sure an easy, yet unefficent way is to create a new auxthread everytime you need access to it, but id like the aux thread to stay alive and only be created once the whole program, be shareable amongst the clients (all threads can call it) and lay idle when not in use..
Anyone have any solutions.. Im sure theres probably a class or simple soltuion to this but im over thinking it..
Thanks again
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty easy. In pseudocode (mostly I just left off exception-related stuff, and I don't show the fictional WorkQueue and WorkUnit classes):

All of the methods of WorkQueue are synchronized. Anyway, the idea is that if there's no work to do, AuxThread calls wait() on a convenient object. The method that gives work to AuxThread calls notify() on that same object. The notify() call makes the wait() return, which makes the running thread do the work, then call wait() to wait for more work.
Doug Lea's Concurrent Programming in Java is a really good source for information about multithreaded architectures. He's got several useful implementations of WorkQueue in there, too.
[ September 12, 2003: Message edited by: Ernest Friedman-Hill ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!