I have some experience with threading in java, but I am wondering…
What’s the best practise to store multiple threads where I can access them both individually and as a group?
My own solution is to create a threadArray class but naturally I would prefer a native class that is much more reliable.
Thanks in advance!
edit
Apparently the functionality is of great importance of the best method. Well, I’ll give an example:
I’ve got an application that basically searches through a lot of information at the same time hence I’m using threads. The threads however each need to perform just a part of the entire operation so I wish to add additional parameters to specify a range.
When a thread finishes it’s specific search it can just stop naturally. When a thread finds a result however, I wish to stop all threads and retrieve that result.
Does that help?
I’d use an
ExecutorServiceto manage my threads as a pool and put theFutures I am given when adding myThreadsto the pool in some form ofCollection.In this way you can manage all of the threads as one unit via the Executor and track individual threads through their
Future.Edit in response to yours
You can use the
shutdownNowmethod of theExecutorServiceto abort all running threads.Example (not a solution to your problem but covers most benefits of using Executors):