• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads starve! Shouldn't yield() or wait()/notify() help?

 
steven brown
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to learn threading and don't understand why in the following code segment one thread always works and the other two starve:
If I replace yield() with both wait() and notifyAll() the other two still starve. Obviously this is a conceptual issue here: What bad assumptions am I making?
BTW does it matter that this runs on a Win98 platform?
Thanks!
public class SleepTest implements Runnable{
public void run() {
while (true){
sleepInsteadOfWaitNotify();
}
}
int i=0;

synchronized void sleepInsteadOfWaitNotify() {
while (true) {
System.out.println(Thread.currentThread().getName() + "Go to sleep");
try{
System.out.println(i++);
Thread.yield();

}
catch (Exception e){
System.out.println(e);
}
System.out.println(Thread.currentThread().getName() + "wake up");
}

}
public static void main (String[] args){
SleepTest t = new SleepTest();

Thread t1 = new Thread(t); t1.setPriority(6);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);

t1.setName("t1");
t2.setName("t2");
t3.setName("t3");

t1.start();
t2.start();
t3.start();

}
}
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep(), yield() does not release the lock/monitor, that is why call sleep(), yield() in a synchronized block or method usually is a bad idea.
For a lot of FAQ on Thread, from basic, medium, hard ones to proctical examples, see here
http://www.webappcabaret.com/javachina/faq/05.htm
Large amount indented, readable, Runable code included.

JavaChina has been moved to http://javachina.developergroup.org/
 
steven brown
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
So if sleep() and yield() don't give up the monitor, to let other threads in, how can it be done?
Thanks,
Steven
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Object.wait(), notify(), notifyAll() instead. That is what they are designed for. Try to read something before you ask more questions, either Sun's tutorial, above link, or javadoc, please!
 
steven brown
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read before asking-
The original question I asked is why doesn't wait() combined with notify/notifyAll keep the threads from starving?
 
Roseanne Zhang
Ranch Hand
Posts: 1953
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your original question: No.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any reason for starvation except for the fact that you set a priority level. You should leave priorities alone completely. Its still not a very good program, but there should be no starvation. win98 does not really matter. if you had winNt and dual processors, you would probably not see what appears to be starvation. or you might because you played with the priorities.
 
steven brown
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gilbert,
So your thought is that if you run that program on a different platform it would not starve..
How would you stop starvation here if running on the same Win98 platform? I had no luck..
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original question stil appears to go partly unanswered. One bad assumption you are making is that thread scheduling is "fair". In actual fact, Java gives you very few guarantees about the way threads are scheduled; presumably to make implementation on a wide range of platforms possible. As RZ's FAQ points out you certainly don't get any guarantees that threads won't starve.
What notifyAll() does is make all wait()ing threads ready to run. It does not actually make them run, relinquish the monitor lock or make the currently running thread yield(). If you don't force the high-priority thread in a state where it is no longer ready to run, for instance by performing blocking I/O or sleep()ing (outside the synchronized method) it is likely to always keep running in preference to the lower-priority threads. In fact, on some platforms this may be the case even if all threads have the same priority.
- Peter

[This message has been edited by Peter den Haan (edited October 22, 2001).]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by steven brown:
Gilbert,
So your thought is that if you run that program on a different platform it would not starve..
How would you stop starvation here if running on the same Win98 platform? I had no luck..

No, what I am saying is that what you are seeing is not starvation. On a preemptively multitasking OS such as win98 or winNT, you will see some thread switching, and threads will generally not starve. Actually I suppose you could call it starvation since the other threads can not get their hands on the resources they need to run.
This is what you want to see, I am not going to completely improve your methodology, but I am just trying to show you the issue in terms of your code

notice that Thread.yield is now outside of the synchronization. So if the OS actually uses thread yield to change to another thread, that thread can get its hands on the resources it needs since the lock is released. Now you will likely see, with cooperation from the OS, one thread then the other. that does not mean it will be fair between 3 or more threads though since fairness is not guaranteed.
This scheme does not involve notify or sleep. Truthfully you will not likely use yield at all, but notify().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!