Android (Java) Concurrency
by CodeStream on Oct.11, 2010, under Android Development
Interested in expanding your knowledge of multithreading? Do you already know a little bit about Runnables and Threads… They’re not very streamlined all on their own are they?
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

August 9th, 2011 on 2:33 pm
Good article! I noticed on your report of completed tasks you call to AtomicInteger.getAndIncrement() you add a 1 to the result. I’m assuming that’s an oversight, but I could be wrong.
Jeff
August 9th, 2011 on 2:47 pm
Hey someone actually read my article =p
Going by what the API says, I believe the getAndIncrement() is returning the old value rather than the new one. I just added the +1 to report which increment was just completed.
Here’s a link:
http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html#getAndIncrement()
August 9th, 2011 on 2:59 pm
I think it retrieves the value and automatically increments but that can be easily verified. I just want to say that you guys have a very cool website. Looking forward to seeing more applications from you. I wish you much success!
Jeff
August 10th, 2011 on 1:29 am
Correction … I see that it increments but it returns the previous value. My bad!