Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help me!  RSS feed

 
guoyuan zhao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class s implements Runnable{
int x=0,y=0;
int addX(){x++; return x;}
int addY(){y++; return y;}
public void run()
{
for(int i=0;i<10;i++)
System.out.println(addX()+" "+addY());
}
public static void main(String args[])
{
s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
when I compiled it and run it,the result is like this
1 1
2 2
.....
20 20
why does it lead to this result?didn't I set i<10?pls help me! zhaoguoyuan@sina.com
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by guoyuan zhao:
why does it lead to this result?didn't I set i<10?
Indeed. And you're running two Threads which do 10 iterations each, giving a total of 20 iterations. Isn't this exactly what you'd expect?
- Peter
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guoyuan
To expand aon what Peter said:
You create one s object which you supply to the Thread constructor. That object has one x variable and one y variable. When you run the 1st thread it loops 10 times and increments both x and y ten times. Then when you run the second thread it increments x and y (the same x and y) 10 more times each.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Chris Linde
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: I have read that when a Thread enters a method it gets it's own copy of that method's local variables. The loop counter variable(i) is local to run(). Is it the case that this code produces 20 pairs of values because there are TWO copies of i (and therefore, 20 executions of the println() statement). Can someone please correct me if I am wrong and elaborate.
Chris says thanks.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. You get a fresh copy of method-local variables every time the method is called. Not only when different threads call the same method, but also when the method calls itself in the same thread (recursion).
So yes, in this case there are two copies of i, each of which goes through its 10 iterations independently.
- Peter
[This message has been edited by Peter den Haan (edited October 31, 2001).]
 
nischal desai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friend,
actuially You have start first thread which increase x and y to 10.after that you give same object to next therad which increase x and y from 10 to 20.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nischal desai:
actuially You have start first thread which increase x and y to 10.after that you give same object to next therad which increase x and y from 10 to 20.
In what sequence the threads do their update in this case is actually completely undefined. A JVM without pre-emptive threading will work the way you say. On a JVM with pre-emptive threading, if you let "i" run through enough iterations, the threads will take turns executing.
But don't believe me - try it. Modify the System.out.println() to show the current thread as well (Thread.currentThread()), increase the number of iterations and see what happens.
- Peter
[This message has been edited by Peter den Haan (edited October 31, 2001).]
 
Tiffany Gorch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here's a question. Could the output have looked something like:
1 1
2 3
3 2
...
My educated(?) guess is "yes"
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may well happen. In fact, the Java language gives you very few guarantees on what the output of this code would be. Specifically, in the absence of synchronization it may take arbitrarily long for updates of non-volatile variables to become visible to other threads. So a perfectly valid JVM may produce the output
1 1
2 2
1 1
2 2
3 3
4 4
3 3
4 4
...
If the threads don't get to see each others' updates of x and y. You will always get 20 iterations however If the updates happen out of order, you may get effects such as the ones you quote.
None of these exotic things are likely to happen on your run of the mill Windows box though.
- Peter
[This message has been edited by Peter den Haan (edited October 31, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!