Results 1 to 2 of 2
- 01-26-2010, 01:17 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
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:
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.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++; } }
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 01:20 AM. Reason: code tags added
-
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
-
Gui problems
By bulldog in forum Advanced JavaReplies: 1Last Post: 12-11-2009, 12:35 PM -
Problems here
By Keno777 in forum New To JavaReplies: 2Last Post: 11-13-2009, 11:35 AM -
SWT OpenGL snippet: draw a square
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:29 PM -
J2ME MIDlet snippet
By Java Tip in forum Java TipReplies: 0Last Post: 11-21-2007, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks