This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread.sleep. What  guarantees? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread.sleep. What  guarantees?" Watch "Thread.sleep. What  guarantees?" New topic
Author

Thread.sleep. What guarantees?

Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
Hello.

If I have:

class X extends Thread {
int i;

X(int i) {
this.i=i;
}

public void run() {
System.out.print(i);
Thread.sleep(5000);
System.out.print(i);
}

static void main(String []) {
X x1=new X(1);
X x2=new X(2);

x1.start();
x2.start();
}

}


I can expect the output to be 1212 or 2121?
Or thread doesn't guarantees nothing and the result is unpredicted?
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
1212
if there was no sleep, you probably can't guess


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thread 2 could be scheduled to run before thread 1 so 2121 is possible. As is: 1212, 1221 or 2112. In other words no guarantee whatsoever.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
Then there is any guarante, isn't it?
Although there are only two threads I can't assume that if one is sleeping for 5 seconds the other get the CPU? This changes if the other thread has more priority?

In general, how can I be sure that another thread will get the cpu? (only with synchronized,wait,notify), with yield/sleep with priorities?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Basically the idea of threads is to get lots of things going on at the same time (either apparently on one CPU or really on a multi-CPU computer). wait, notify, notifyAll are used to keep threads from corrupting common resources. yield, sleep, priorities, join are used to suggest to the thread scheduler (implementation, operating system, hardware dependent) a scheduling order. In my opinion you should program threads with that in mind - you want things to happen at the same time - and not be a thread scheduler.


You may be interested in the Java 5.0 java.util.concurrent package classes to see what you can use for sharing resources between threads (also see Henry Wong's Java Threads book). The new classes are not part of SCJP 5.0 however (a shame).
[ October 20, 2005: Message edited by: Barry Gaunt ]
Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
I know what you mean. In the real world I think that if you yield() or sleep() and you have two threads of the same priority you will obtain concurrency in practically any platform and situation, and that's all.

But in the exam, sometimes you need to do be a thread scheduler, don't you?
And my question was mainly with the exam in mind.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For the real examination I think it's fair to say that you will have to choose between a clear case of nondeterministic behaviour (unsynchronized or synchronized on distinct objects) and deterministic behaviour synchronized on one object (explicit or implicit).
You should know that no guarantees exist for yield, sleep, priorities.
Also that calling the run method of a thread does not change thread context.
[ October 20, 2005: Message edited by: Barry Gaunt ]
 
Don't get me started about those stupid light bulbs.
 
subject: Thread.sleep. What guarantees?
 
Similar Threads
Please critic, rate or give some helpful advice
"new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool
use of yield() ???
Solution for deadlock
yield() method