• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difficulty in understanding ThreadPools.

 
Srini Rangu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,
My apologies if this is being posted in the wrong forum.
I have been a regular reader of Javaranch for some time now, and I found this community to be of great help.

I have been trying to get myself familiar with new features of Java Tiger, and I am having a bit of a tough time understanding ThreadPools. I'd very much appreciate if anybody could point me in the right direction.

If I understand the implementation correctly, when I say

the pool that I have created will reuse my Task. I would like to understand how can I give the Runnable a new message and get the output upon execution. I mean, how can I pass input and capture output, in my case I give an xml string parse it and populate a Map. Since there are no accessor methods to get a free task from the pool.

would you please suggest a book that covers concurrent utilities in detail.

Thanks very much for you time.
Srini
[ March 28, 2005: Message edited by: Srini Rangu ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch! There is a special threads forum up the page for this kind of question, but I'll answer here anyhow.

You can pass anything you like to your runnable, either in the constructor or in set methods before you give it to the Executor. So feel free to give it any arguments it needs to do the job.

Getting results when the runnable is done is trickier. I'd probably give it a reference to a result collector and have the runnable call the collector with results when it is done. I'm not sure how to know when all runnables are done, or even to define what that means for sure. You could always add another one! Maybe count how many you put into the pool and how many results you get back.

Make sure the collector is thread safe as many runnables might try to submit their results all at once. A synchronized method ought to do.

Hmmm, why not just pass a synchronized collection in the first place? This Collector is not adding much value.

Let me know if that helps!
[ March 28, 2005: Message edited by: Stan James ]
 
Srini Rangu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch! There is a special threads forum up the page for this kind of question, but I'll answer here anyhow.

Thanks very much. I have seen a lot of people actively discussing new features in this forum, so I thought I might have a better chance of getting answers here. If this post is not appropriate here I'd request sombody to please move it to the Threads forum.

I have some custom tasks created and stored them in a LinkedList(taskPool). I also have a another LinkedList in which I store the reference of a task that has alredy been submitted to the Executor for execution(busyTaskPool). when my taskPool becomes empty I iterate throught the busyTaskPool and copy all the completed tasks to taskPool again. I'm sure there must be a better way of doing it, I'd very much appreciate if somebody points me in the right direction.

here's my code


Thanks,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!