Results 1 to 2 of 2
- 02-04-2008, 06:08 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
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?
Java 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; } }
- 04-23-2008, 06:30 PM #2
Lots of typos in the code, that code won't compile! Why is the variable supposed to be protected by the synchronization public and static. How is each thread initialized with the instance of the synchronized object? Synchronization works, don't worry about that. You have bugs in the code. Post the whole working code and we might help.
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Similar Threads
-
synchronization question
By oguz in forum Threads and SynchronizationReplies: 2Last Post: 07-22-2008, 08:56 AM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM -
is synchronization on method passing local variables as parameters needed
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 04:47 PM -
Synchronization problems
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 01:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks