*
The moose likes Java in General and the fly likes Local variable and Thread issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Local variable and Thread issue" Watch "Local variable and Thread issue" New topic
Author

Local variable and Thread issue

Bhavik Shahm
Greenhorn

Joined: Apr 12, 2012
Posts: 3
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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local variable and Thread issue
 
Similar Threads
Please Explain working of this program.
Wake up a thread
Getting Session on web service client
Another Simple Java Question
Trying to get manageable arrays to work with