Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Sleep Zero  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this statement have any performance impact:

int x=0;

try{
Thread.sleep(x);
} catch(InterruptedException ie){...}

In my loop x is sometimes zero.

Would it be better to to say:

if(x != 0){
try{
Thread.sleep(x);
} catch(InterruptedException ie){...}
}

Thanks!

Drew
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drew: Does this statement have any performance impact?

It actually depends on how frequently the code of yours is executed. Going by what you have specified, i do not think this will be done per each request and will be done in a scheduled manner. So, it will not be a performance hit for sure.
However, I think you can do that check as making a Thread sleep result in a native call that removes the thread from the OS scheduling system. So, it is some work for the JVM and it will be very angry at you if after doing all this work, it realises that the sleep call was not required
[ March 18, 2008: Message edited by: Nitesh Kant ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!