aspose file tools
The moose likes Threads and Synchronization and the fly likes Variable increment problem (threads) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Variable increment problem (threads)" Watch "Variable increment problem (threads)" New topic
Author

Variable increment problem (threads)

Naveen Koneti
Greenhorn

Joined: Apr 27, 2007
Posts: 13
Hi All,

I have a problem in my project.Some thing like I will be spawning n number of threads.These threads have a increment two variables x and y.And these threads have to run infinitly each time incrementing and using those variables.After some time I am getting these variables being increments out of sync though i am incrementing them in a syncronized method.
ex: if I give initial variables as x = 100 and y =200.after running the program say after 5 min those variables are becoming something like 144 & 251 but I am expecting them to be 191 & 291.
my method is like this

static long x = 0;
static long y = 0;
public void run()
{
while(true)
{ increment();//increments the variables }
if(someCondition)
{ thread.yield(); return;//terminating execution }
}

static synchronized increment()
{
x++;
y++;
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

They should appear to be in sync to any thread that reads them in a matching synchronized method or block.

Please don't post the same question in multiple forums. I'm going to move this one to the Threads forum and delete the other copy.


[Jess in Action][AskingGoodQuestions]
Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
Originally posted by Naveen Koneti:
Hi All,

I have a problem in my project.Some thing like I will be spawning n number of threads.These threads have a increment two variables x and y.And these threads have to run infinitly each time incrementing and using those variables.After some time I am getting these variables being increments out of sync though i am incrementing them in a syncronized method.
ex: if I give initial variables as x = 100 and y =200.after running the program say after 5 min those variables are becoming something like 144 & 251 but I am expecting them to be 191 & 291.
my method is like this

static long x = 0;
static long y = 0;
public void run()
{
while(true)
{ increment();//increments the variables }
if(someCondition)
{ thread.yield(); return;//terminating execution }
}

static synchronized increment()
{
x++;
y++;
}


Have you tried making x and y volatile?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Volatile will not help here - you may read the values when x has been incremented but y has not. EFH's answer is correct:

Because this method is synchronized the same way the increment() method is, it should always see the same values for x and y. (Assuming there are no other unsynchronized methods that change x and y.)


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Variable increment problem (threads)
 
Similar Threads
Threads again
Variable increment problem (threads)
Help with counter
static block understanding