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 } }