• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ThreadDoubt

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic