This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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?
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.
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.
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.
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.....
Joined: Jan 09, 2008
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.