• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the differnt between Thread Sleep n Thread No sleep  RSS feed

 
Akmal Zahdi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiii every one!

Actually I'm study java code with doing coding in Code Barn in this web site. There is 2 type of code that make me confuse about using thread. It's HelloThreadTwo and HelloThreadTwoNoSleep. There is explain about Sleep n No Sleep but I cann't see what is the different between them. Yes, it different when u run the two applet, it's different. What make me confusing is the concept using Thread Sleep n No Sleep. Can anyone who is understand this concept explain to me. All of ur corporation i'm realy2 appreciate it. tq.
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the Code here ?
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extract from the code barn
Without being "forced" to sleep, the Threads will not necessarily take turns. If you can run this applet on both a Macintosh and a PC, you might see a DRASTIC difference. On a PC, thread scheduling often works the way you might hope: the Threads appear to take turns, more or less. But on a Macintosh, one Thread might just stay in until it has finished!

WARNING: turn-taking is NOT built into Java! The responsibility is for you, the programmer. So while you might get good behavior on a PC, you can't guarantee it across all Java platforms. To guarantee fair Thread behavior (proper turn-taking) you must use a strategy which employs yield() and/or sleep(). So if this NoSleep example runs on your machine exactly like the ThreadTwo example (which DOES sleep), you're lucky. Nothing programmed in the Java applet is making that happen.

Bottom Line: If you're just starting with Threads, ALWAYS USE SLEEP in your run() method to be safe. Later, as you become more experienced with Threads you may use more sophisticated scheduling strategies.
 
Akmal Zahdi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Nigel Browne for that statements. I already read it but still not get it . . For ur info, I'm not really good in english. But this one it seem so complicated to understand. I want to know what concept are using in Thread No Sleep and Thread Sleep. By the way thanx.

Well for Sanju Thomas, the complete code u can see it in Code Barn that provide from this Javaranch Web Site. The code is HelloThreadTwo and HelloThreadTwoNoSleep. I'm already looking for that codes. What's the different between this code is like what i write below

for HelloThreadTwo

public void run()
{
// this run method will repeat 10 times
for (int i=0 ; i<=10 ; i++ )
{
myList.add (Thread.currentThread().getName() + " is running");
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{

}
}// close for loop

myList.add (Thread.currentThread().getName() + " is running");

}// close run

for HelloThreadTwoNoSleep

public void run()
{
//this run method will repeat 10 times
for(int i=0;i<10;i++)
{
myList.add(Thread.currentThread().getName() + " is running");

}//close for loop
myList.add(Thread.currentThread().getName() + " is done");
}//close run()

from this, what i can the say the different between this 2 code is only in run() method.Do u(Sanju Thomas) know about this or anybody can explain about this for me. That's all. Tq.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well basically, in the example HelloThreadTwoNoSleep the threads created are in direct competion with each other for processor time. It doesn't matter how we think the threading order should be it is out of our control and up to the operating system and processor you are running the code on.
In the other example HelloThreadTwo the threading code has been written to define a specific order of Thread creation so we have control and won't be surprised by threads not behaving as expected.
For more information on thread priority readthe Thread lesson from the java tutorial
[ June 01, 2005: Message edited by: Nigel Browne ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!