View Single Post
  #1 (permalink)  
Old 02-04-2008, 07:08 AM
sherinpearl sherinpearl is offline
Member
 
Join Date: Feb 2008
Posts: 1
sherinpearl is on a distinguished road
Synchronization Doesn't seem to work
Hi,

I have a class ThreadBO which starts 5 threads. Each thread is supposed to call a synchronized method 'IncrementVolCount()' which just increments a count variable & return the count variable to the thread. A stub code is given below. But when i run this i see that mutilple threads prints the same count. Could you please let me know if there's anything wrong in the way i have implemented synchronization/threading?


Code:
public class ThreadBO{ public void compute(){ //Create & Start Volume thread Objects. volThreadObj = new RSThread[5]; for(int intThreadCount=0;intThreadCount<5;intThreadCount++){ volThreadObj[intThreadCount] = new RSThread(); volThreadObj[intThreadCount].start(); } } } public class RSThread extends Thread{ public void run() { count = intUtilObj.IncrementVolCount(); System.out.println(Count); } } public class InterfaceUtils { public static int intVolCount=-1; public synchronized int IncrementVolCount() { return ++intVolCount; } }
Reply With Quote
Sponsored Links