Results 1 to 2 of 2
  1. #1
    QkrspCmptPop is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default what are the problems of the snippet

    my purpose is to implement a counting semaphore (one concurrency control object) in java. (I know it is available in java.util.concurrent, but this is the requirement).

    Here is the code:
    Java Code:
    public class countingSemaphore{	
    	private static final int _MOSTTABLES = 3;
    	private static int availtable = _MOSTTABLES;
    	private static final Object lock = new Object();	
    	
    	public synchronized static void Wait(){		
    		while(availtable==0){		
    			try{
    				synchronized(lock){
    					lock.wait();
    				}			
    			}
    			catch(InterruptedException e){
    				e.printStackTrace();
    			}
    		}
    		availtable--;
    	}
    	
    	public synchronized static void Signal(){
    		while(availtable==_MOSTTABLES){
    			try{
    				synchronized(lock){
    					lock.notifyAll();
    					}				
    			}
    			catch(InterruptedException e){
    				e.printStackTrace();
    			}
    		}
    		availtable++;		
    	}
    }
    The synchronized static methods lock resource sharing among instances of class. The synchronized within methods is to use wait/notifyall mechanism for a static level method.

    But these codes still look strange though I do not figure out the strange reason.
    Can anybody tell me why? Or any other issues I have to take care of here in order to implement the counting semaphore. Thanks.
    Last edited by Fubarable; 01-26-2010 at 02:20 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello!

    I'm no pro at semaphores, but I am a pro at code tags, and so I hope you don't mind that I added them to your code. Please see my signature below if you want to do this yourself in your next posts.

    Good luck, and I hope you resolve this issue soon!

Similar Threads

  1. Gui problems
    By bulldog in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2009, 01:35 PM
  2. Problems here
    By Keno777 in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 12:35 PM
  3. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:29 PM
  4. J2ME MIDlet snippet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-21-2007, 12:41 PM

Posting Permissions

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