klamath
12-13-2000, 01:14 AM
I'm having a tough time getting my head around the idea of a thread pool (I understand the concept, just not how to implement one in Java). Here's what I've got at the moment.
I have a ServerPool object, which is created and passed an int 'n'. It creates 'n' threads (instances of FooThread, which implements "Thread"), and stores them in a LinkedList (or whatever other data structure). When the dispatch() method of ServerPool is called, it finds the first thread in the pool (in this method, access to the pool is synchronized) -- if it can't find a thread, it creates a new one and puts it in the pool. It then removes the thread from the pool and calls thread.run()
I have implemented everything up to that point. The thread should now execute -- but how can I put the thread back inside ServerPool's LinkedList of available threads after execution? How can I do stuff at the end of Thread.run() -- doesn't the thread terminate automatically when Thread.run() finishes? Basically, I'd like to avoid this behavior -- when Thread.run() finished, I'd like the thread to be placed into the pool again. Am I going about this the wrong way, or have I just missed something obvious?
I'd be happy to provide code if you'd like.
Thanks in advance.
------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Better Bulletin Board (http://bbb.sourceforge.net)
I have a ServerPool object, which is created and passed an int 'n'. It creates 'n' threads (instances of FooThread, which implements "Thread"), and stores them in a LinkedList (or whatever other data structure). When the dispatch() method of ServerPool is called, it finds the first thread in the pool (in this method, access to the pool is synchronized) -- if it can't find a thread, it creates a new one and puts it in the pool. It then removes the thread from the pool and calls thread.run()
I have implemented everything up to that point. The thread should now execute -- but how can I put the thread back inside ServerPool's LinkedList of available threads after execution? How can I do stuff at the end of Thread.run() -- doesn't the thread terminate automatically when Thread.run() finishes? Basically, I'd like to avoid this behavior -- when Thread.run() finished, I'd like the thread to be placed into the pool again. Am I going about this the wrong way, or have I just missed something obvious?
I'd be happy to provide code if you'd like.
Thanks in advance.
------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Better Bulletin Board (http://bbb.sourceforge.net)