Results 1 to 2 of 2
  1. #1
    sherinpearl is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

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

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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

  1. synchronization question
    By oguz in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2008, 08:56 AM
  2. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  3. Replies: 0
    Last Post: 11-10-2007, 04:47 PM
  4. Synchronization problems
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •