• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variable and Thread issue

 
Bhavik Shahm
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to run following sample java program but output is not coming proper.



The Output comes to

inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
num rec0

where as expected output is

inside num rec10
inside num rec20
inside num rec30
inside num rec40
inside num rec50
inside num rec60
inside num rec70
inside num rec80
inside num rec90
inside num rec100
num rec100

whenever a numRecis passed to method execProc it is passed as zero where as it should be passed as the last incremented value.

 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavik Shahm wrote:Hi,

I am trying to run following sample java program but output is not coming proper.



The Output comes to

inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
inside num rec10
num rec0

where as expected output is

inside num rec10
inside num rec20
inside num rec30
inside num rec40
inside num rec50
inside num rec60
inside num rec70
inside num rec80
inside num rec90
inside num rec100
num rec100

whenever a numRecis passed to method execProc it is passed as zero where as it should be passed as the last incremented value.




This is *not* a threads issue. Remember that Long objects are immutable, so when you increment, you are unboxing, incrementing the primative, and reboxing to a different object.... now consider what would happen when you do this to method parameters?

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic