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 yield and sleep 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 "yield and sleep " Watch "yield and sleep " New topic
Author

yield and sleep

denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Hey java ppl
read this code carefully and confirm me if i am right??

class Callme {
void call(String msg) //line 1
{
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (Exception e){};
System.out.println("]");
}
}
class caller implements Runnable {
String msg;
Callme target;
public caller(Callme t, String s) {
target = t;
msg = s;
new Thread(this).start();
}
public void run() {
target.call(msg);
}
}
public class Synch {
public static void main(String args[]) {
Callme target = new Callme();
new caller(target, "Hello");
new caller(target, "Synchronized");
new caller(target, "World");
}
}

prints:
[Hello[Synchronized[World]
]
]
if used with synchronized key word with the method declaration at line one it prints
[Hello]
[Synchronized]
[World]

Also the same thing happens if in the method call() in the class Callme, yield() method is used

Depending on the above examples can i safely conclude that calling sleep() and yeild() the threads
do not relinquish the control of the particular monitor that they have on a specifec object ???
And only calling wait() will do so .

Regards denish
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Denish,
You're right. Neither <code>sleep()</code> nor <code>yield()</code> give up the lock. <code>wait()</code> will.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
srinivasrao marri
Ranch Hand

Joined: Feb 13, 2001
Posts: 61
hi dinesh,
as method yield() what it does is, it brings u'r currently running thread for running state to ready-to-run, where as the sleep() method will makes the running thread to blocked state/sleeping sate for the specified amount of time. during this sleeping time the thread will be scheduled another task. after the completion of the specified time. the thread will goes to the readytorun state but not directly to the running state. from the readytorun state the thread scheduler allocates the thread to running state based on the thread's priority. so we should not conclude these methods tobe same.
at the maximum what we can conclude is the methods wait(), yield() and sleep() will stops the thread from running state to other state.
thankyou
srinivas
denish mehta
Ranch Hand

Joined: Dec 21, 2000
Posts: 135
Thankc a lot guys
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Good posting!!! That was a big help to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: yield and sleep
 
Similar Threads
Question on synchronize block
Threads and sleep
thread's
synchronized is not working. why ?
Pls explain the program