aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ThreadDoubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ThreadDoubt" Watch "ThreadDoubt" New topic
Author

ThreadDoubt

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361

How is the output in the above code coming to be 0,2,4,6,8,10,12,14,16,18,20,22
I think it shld be something like 00,22,44,66,88,1010. but how is it managing to go beyond 6 iterations?? there is one object made of inner class and it is Given to two seperate threads, they are both attacking 1 run() method. so the variables shld be shared. I can understand that the variable x is being shared for both threads, but how is the variable in the for loop 'i' being reset after the first thread has finished its run()? Its not even like the run() is synchronized.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Given to two seperate threads, they are both attacking 1 run() method. so the variables shld be shared.


What variables should be shared? The local variables in the run method?

The only shared variable that I can see is x in class ThreadDoubt.


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

Joined: Aug 07, 2006
Posts: 54
public class ThreadDoubt{
int x=0;
public class Runny implements Runnable{
public void run(){
int curr=0;
for(int i=0;i<6;i++){
curr=x;
System.out.println(Thread.currentThread().getName() + ": " + curr + ",");
x=curr+2;
try {
//Thread.sleep(10);
} catch (Exception e) {}


}
}
}

public static void main(String[] args)
{

new ThreadDoubt().doStuff();
}
public void doStuff(){

Runnable r=new Runny();
Thread one = new Thread(r);
one.setName("Fred");
one.start();
Thread two = new Thread(r);
two.setName("Lucy");
two.start();

}
}

I added some names to the threads so you can see which of the two threads(workers)is accessing the single runnable(job). They basically run one after the other on my machine, but this is not gauranteed with threads. If you uncomment the sleep() code they will alternate working on the run() method and the output is still the same.
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
I added the sleep method but still the output is coming as same.i am not able to understand . why the second thread runs after the end of first thread.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Hi thanks for your inputs. I looked the whole thing over and think i get it. The problem I was having was understanding why the run()'s variables werent being shared being that there was only one object of the runny class. I was confused in that I could see that there was only one instance of the outer class and therefore its variable X was being shared and that made sense, but if there was only one instance of the inner class then how come the run()'s variables and for loop wasnt also being shared. I was expecting each thread to compete with the other and the total iteration count to not go more than six. But I guess when you hand a thread a job to do then each thread has to finish its own job and the loop will iterate the specified number of times for each thread.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
how come the run()'s variables and for loop wasnt also being shared.


The local variables in the run method are never shared. Each thread has its own stack of local variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadDoubt
 
Similar Threads
CertPal #33 on Threads
Insights into synchronization
Confusion in threads
Threading
Doubt in Thread