views:

157

answers:

3

I learnt Java back in university. It's been 4 years since I last coded Java. I develop PHP applications mainly. This time I need a language with more powerful concurrency support. I thought to myself, I'll just revise my Java in an hour and I'm ready to go.

As it turned out, there is no human friendly tutorials (!!) that can be easily found. I searched "java tutorial" and the first results are either impossibly abstract with no useful code examples or ad-filled spit outs of the Web 1.0 era! More complex searches just led me to more confusing/outdated posts. I just love PHP for the numerous friendly tutorials out there.

Anyway, to avoid making this a pointless post, can anyone direct me to a readable tutorial to how I can use the thread ExecutorService to 1) queue a few thousand Runnables, 2) have a maximum of 15 threads executing at a time, and 3) if a thread fails, re-queue it or just don't remove it from the Executor's pool.

Thank you in advance!

+2  A: 

It's from 2006, but still pretty decent: http://math.hws.edu/javanotes/

Amber
Thanks for the link - seems more useful than all the ones I found so far! :)
Amer
@Amer, then perhaps the problem lies not so much with the tutorials available, but with your weak google-fu.
paxdiablo
+1  A: 

If you dislike Times New Roman, just change the browser default font to Tahoma or something like.

alt text

Then start here and click your way through Next link. Then there are the API docs, each with examples in the introductory text. E.g. ExecutorService. Then there are books, like Concurrency in Practice.

BalusC
I think the Times New Roman is one of the issues :P Also, I guess I'm spoiled by the readiness of so many functions in PHP!Now looking for how to use ThreadPoolExecutor to assign a time out for the threads, but not drop them entirely off the queue.Thanks
Amer
Noted should be that PHP is not really comparable with Java. It's a hypertext processing language. Designed with webdevelopment in mind. Webdevelopers are more sensitive to good-looking sites. Also don't forget that you've in Java insanely much more choice of 3rd party libraries and that they are easy integrable.
BalusC
Hehe... can't help it :)
Amer
+2  A: 

this should work for you

Executors.newFixedThreadPool(15) 

15 threads fixed, not max. but it's no big deal to have 15 idle threads.

regarding tutorials: good tutorials are very hard to write, and generally there is NO reward.

regarding concurrency: it is very difficult. people spend years just to understand the basics. you shouldn't be surprised if a few hours is needed to understand a concurrency issue.

irreputable
You are right, no rewards, but Sun and now Oracle need to take better care of their product. Why not have a "Learn Java in 10 days" tutorial like PHP Symfony?I didn't do much coding with concurrency before, so that might by why I'm a bit confused.I used your suggestion, and now working on getting the Exception handling right, not to skip executing one of the threads. Any ideas?As for the idle threads, the shutdown() method should clean things up with a timeout value.
Amer
@Amer - once again, where is the value proposition for Sun / Oracle paying for a team of tech writers to work on their free online tutorials? If they are not good enough for you, fork out some money and buy a book or attend a Java training course.
Stephen C
@Ames, also 10 days is perhaps a bit low. You should read this: http://norvig.com/21-days.html
Thorbjørn Ravn Andersen
@TRA: Amen, brother. I'd ask Amer: How long did it take you to learn PHP? If it was more than ten days, why on earth would you think that any other language would be so easy? ("Hello, NASA? I'm taking a week's vacation, and I'd like to become an astronaut by the time I go back to work....") Crazy.
duffymo