Java introduced a package in 1.5 (I think) which went unknown to me for a while. Wish I’d found it sooner because I was doing things the hard way, or rather, the more meticulous way. So what is this package called? :
java.util.concurrent <- Here’s an api link for those who’d like to learn about it that way.. be warned it’s a pretty large bit of info to take in all at once
What does this package offer?
- Automatically manage a pool of threads
- Use a fixed thread count or..
- Create as many threads as needed, reusing threads when possible
- Start up single tasks normally, while manage them under your thread pool
- Blocking until a pool of threads are all finished
- Optional timeout period
- Repeating tasks at a rate asynchronous to their execution time!
- Yes, this means it’s perfect for timers.
- Or wait until a specific elapsed time AFTER execution
- Return values, or Throw exceptions from Threads
- Shut down thread pools and single tasks with many options
- Shut downÂ in an orderly fashion by rejecting new tasks but allowing existing tasks to complete
- Quickly shut down by interrupting tasks in progress
- Optionally block while waiting for a shut down
- Optional timeout period if chosen to block during shut down
- See if a task was cancelled or if it finished
- +Plus tons of even cooler stuff