Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on thread sleep method  RSS feed

 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java and trying to execute the program below
public class Moo {
public static void main(String[] args) {
try {
runner run = new runner();
run.start();
Thread.sleep(6*100);
run.cont = false;
run.value = 5;
Thread.sleep(6*100);
run.cont = false;
run.value = 7;
Thread.sleep(6*100);
}
catch (Exception e) {}
}
}

class runner extends Thread{
public boolean cont = true;
public int value = 3;
public void run() {
while (cont) {
System.out.println("running in a loop - " + value);
}
}
}

When i run the program "running in a loop -3" is printed infinite times. Can't really understand what's going on with this code? please help
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all: use code tags
Second: when you spawn a new thread, the thread's instance variables may be cached. So when another thread changes it, the first thread may not see the change, but will carry on with the old ("stale") value unless you provide proper synchronization between the threads or make the changed variable volatile. So in your case the declaration:

should fix the problem. If it doesn't, please post. It's weird that on a modern VM the spawned thread never stops - from my experiments the thread always finally noticed that the value had changed... But anyway the cont field (and the value field and any other fields that are changed by other threads directly) should be marked volatile.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was asked the same question in one of my interviews. As per my understanding, The statement running in the loop - 3 will be printed for n number of times and program will be terminated. I assume, the number of iterations cannot be predicted because its completely up to the Processor.
Remember, There might be a chances of context switching between main thread and the runner thread.

Btb, can you please tell me where did you get this question from?

sat
 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam ,

I still get the same output even after declaring Cont variable as volatile.

Thanks
 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi satish,

I found this question in a test which i am supposed to take to get qualified for an interview with the client.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vishnu bola wrote:
I still get the same output even after declaring Cont variable as volatile.

Well that is really strange. Just to be sure - the output "running in a loop -3" is being printed infinite amount of times/forever (or at least for over a minute)? On my computer the same code, with volatile added, prints "running in a loop -3" around 16000 times, but then exits. And it lasts for under 2 seconds. Even without volatile it stops after around 16000 times/2 seconds.
 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam you are right. It's not printed infinite times but some n number of times which i am not sure how many.
can you please clarify why you thought that volatile modifier will make a difference to the output?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of times the message is printed depends on your computer's speed and your OS's thread scheduling policy.
Now, about the volatile keyword: normally, when using multiple threads, each thread may (but does not have to) store its variables (references or primitives) in some kind of cache - processor registers, thread local memory area or some other structure which is faster that direct global memory access. Now when another thread modifies this variable, it may modify only its local, cached copy. This change will not be visible to other threads using that variable. The change may be flushed after some time. But it may even never be flushed. So, in your case it could have been that the "main" thread had its copy of run.cont and runner had its copy. When main changed cont to false, runner could have never seen it. Or it could have seen it after a long time. But it could have seen it almost immediately, too. With non-volatile variables you never know
Here are some links on that topic:
http://www.ibm.com/developerworks/java/library/j-jtp06197.html?S_TACT=105AGX02&S_CMP=EDU
http://pitfalls.wordpress.com/2008/05/25/javavolatile/
http://www.javaperformancetuning.com/news/qotm030.shtml
http://mindprod.com/jgloss/volatile.html
http://www.javamex.com/tutorials/synchronization_volatile.shtml

Another thing is that even with volatile the message is printed a lot of times. The Thread.sleep(600) call allows the runner thread to execute for over 600 milliseconds, which is enough to print thousands of lines
 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Adam. Your explanation is very neat and cleared all my doubts.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishnu,

It sounds great. The purpose of using the keyword volatile is to make the visibility of changes to the variable by one thread to another thread. As I & Adam already mentioned in our previous posts, your runner thread will run as many as times when it gets a chance to run. Because it is completely decided the by Processor and/or OS Scheduler. Eventually both threads will die and quit the application.

sat
 
vishnu bola
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks satish for your reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!