| Author |
Should Threads performing independent tasks sleep?
|
Joe Joseph
Greenhorn
Joined: Jun 21, 2006
Posts: 20
|
|
Hi,
Suppose there are N Java Threads working independently pickings tasks from a Queue and processing in a loop. Is it suggested that each of these Threads sleep for a while?
For example consider a Consumer Producer scenario. A Producer puts tasks in a Queue. There are N Consumer threads polling the Queue in a endless loop (assume that Queue polling is done in the right way such that 2 Threads dont pick the same task) and performing these tasks which are independent. In this case is it suggested that the N Consumers perform sleep(..) or can they be polling & executing tasks in a tight sleepless loop? Would tight sleepless loop cause Thread starvation?
Regards,
Sajee
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
I wouldn't make them sleep, generally. That would just make them take longer to complete. I suppose there might be cases where Thread A is doing things which prevented other threads from doing more important things, but that's what the yield() method is for. However I wouldn't recommend using that either until analysis of your situation was done which specifically pointed to it. Don't just call sleep() or yield() because you think it might be a good idea.
If you google "thread starvation" you'll find you haven't said anything in this thread related to the factors which cause that.
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Rather than looping and polling, this is where the wait() and notify()
methods should be used, with the task queue as the monitor object.
Be sure to research the proper use of these methods. Their use should
always be synchronized on the monitor object. Also, when a thread
picks up a task, it can change its own priority based on the importance
of the task. Good luck.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Joe Joseph wrote:
For example consider a Consumer Producer scenario. A Producer puts tasks in a Queue. There are N Consumer threads polling the Queue in a endless loop (assume that Queue polling is done in the right way such that 2 Threads dont pick the same task) and performing these tasks which are independent. In this case is it suggested that the N Consumers perform sleep(..)
As Jim mentioned, the correction solution is to use wait() and notify() which does put the thread into a non-running state, just like sleep, but only for the exact time to wait for work -- no more no less.
However, for consumer producers, another option is to use a blocking queue, A blocking queue does all the wait() and notify() under that covers, and all the threads need to do is put to and get from the queue.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Joe Joseph wrote:... or can they be polling & executing tasks in a tight sleepless loop? Would tight sleepless loop cause Thread starvation?
It would *not* be a good idea to spin wait, waiting for work to coming in.
Henry
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
correct me if i am wrong
it is possible if we use sychronized method and also if we use join(),,,,,,,,,,
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Phil : Please elaborate on your question.
Jim ... ...
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Jim Hoglund wrote:Phil : Please elaborate on your question.
Jim ...  ...
i am not asking any question.........
i am just giving the answer of above question and wanna know whether i am right or not...........
we can use synchorized method for the solution of above query
and can we also use join() method.....
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Phil: Thread.join() is used to wait for a thread to complete its run() method. If threads
are joined end to end, they will run independently, one after the other. There will be
no need for synchronized because there will be no contention for data. This is not Joe's
problem, however. He wants his threads to work independently, rather than sequentially.
The wait() and notify() methods take a more granular approach. Critical data elements
are locked with synchronized. Another thread will have to wait() for notify() only if it
happens to want the same lock at the same time. Depending on the specifics, this may
not be very often.
Jim ... ...
|
 |
 |
|
|
subject: Should Threads performing independent tasks sleep?
|
|
|