Article View: borland.public.jbuilder.java.language
Article #15812Threading Problems
From: MM
Date: Tue, 20 Apr 1999 00:00
Date: Tue, 20 Apr 1999 00:00
114 lines
3589 bytes
3589 bytes
Hi all,
This is a longish post, so I must apologise. What I am trying to do is to
simulate a Semaphore. For some reason the implementation seems to enter into
a deadlocked state. Could anyone help in finding out why? TeamB?
8<--------------------Semaphore.java------------------------------
/**
* Implementation of the synchronisation primitive
* commonly known as a Semaphore.
*/
public class Semaphore{
int _initialCount;//the max no. of resources this semaphore
// is guarding.
// In other words - the max no of threads
// that have performed the down() op and
// are still in the running state.
int _freeCount;// the no. of times down()can be performed
// without suspending the calling thread.
Object _waitObject;
public Semaphore(int count){
if(count == 0)
throw new IllegalArgumentException("parameter cannot be zero");
_initialCount = count;
_freeCount = _initialCount;
_waitObject = new WaitObject();
}
public synchronized void down(){
--_freeCount;
if( _freeCount < 0 ){
_freeCount = 0;
{
synchronized(_waitObject){
_waitObject.wait();
}
}catch(Exception e){
System.out.println("Exception:");
e.printStackTrace();
}
}
}
public synchronized void up(){
++_freeCount;
if(_freeCount > _initialCount)
_freeCount = _initialCount;
synchronized(_waitObject){
_waitObject.notify();
}
}
public static void test(){
Semaphore s = new Semaphore(1);
Thread t1 = new TestThread(s,1);
Thread t2 = new TestThread(s,2);
Thread t3 = new TestThread(s,3);
t1.start();
t2.start();
t3.start();
for(int i = 0;i<1000;++i);
Thread.yield();
Thread.yield();
Thread.yield();
try{
Thread.sleep(200000000L);
}catch(Exception e){};
}
public static void main(String[] a){
Semaphore.test();
}
}
class WaitObject{}
class TestThread extends Thread{
Semaphore _s;
int _id;
public TestThread(Semaphore s, int id){
_s = s;
_id = id;
}
public void run(){
System.out.println("Before down : " + _id);
_s.down();
yield();
System.out.println("1st After down : " + _id);
yield();
System.out.println("2nd After down : " + _id);
yield();
System.out.println("3rd After down : " + _id);
yield();
System.out.println("4th After down : " + _id);
_s.up();
}
}
8<--------------------------------------------------------------------------
--------
I think that the notifies are getting lost somewhere. Is that correct?
Many thanks
-MM
Technical Details
Message-ID:
<7fhv2i$1ot7@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,7c243bb839140ae0,start X-Google-Attributes: gidf74b3,public From: "MM" <manu_mathew@geocities.com> Subject: Threading Problems Date: 1999/04/20 Message-ID: <7fhv2i$1ot7@forums.borland.com> X-Deja-AN: 468708247 X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.0810.800 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.language