• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Thread design

Posts: 11
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
author and iconoclast
Posts: 24207
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
    Bookmark Topic Watch Topic
  • New Topic