• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should Threads performing independent tasks sleep?

 
Joe Joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21119
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct me if i am wrong
it is possible if we use sychronized method and also if we use join(),,,,,,,,,,
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil : Please elaborate on your question.
Jim ... ...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic