Original Post
Hello people, I've a small problem with my multithreaded server... I've one thread who accepts requests and pushes them into a vector:
private Vector jobList_;
private java.util.concurrent.Semaphore jobListSema_;
private java.util.concurrent.Semaphore jobListEmptySema_;
public void pushJob( JobEntry job )
{
try
{
jobListSema_.acquire();
jobList_.add( job );
jobListSema_.release();
jobListEmptySema_.tryAcquire();
jobListEmptySema_.release();
} catch( InterruptedException e )
{
}
}
With the following initialization in my constructor:
jobList_ = new Vector();
jobListSema_ = new java.util.concurrent.Semaphore( 1 );
jobListEmptySema_ = new java.util.concurrent.Semaphore( 1 );
jobListEmptySema_.tryAcquire();
And i've an other thread, who removes those jobs:
public JobEntry popJob()
{
JobEntry res;
try
{
if( jobList_.isEmpty() )
jobListEmptySema_.acquire();
jobListSema_.acquire();
res = (JobEntry) jobList_.remove( 0 );
jobListSema_.release();
} catch( InterruptedException e )
{
res = null;
}
return res;
}
And now after some time (and about 500 jobs) I get:
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Array i
ndex out of range: 0
at java.util.Vector.remove(Vector.java:793)
at LoungeServer.popJob(LoungeServer.java:173)
at LoungeServer.run(LoungeServer.java:152)
Which is exactly the remove in popJob() But I don't understand how this can happen, as I thought the jobListEmptySema_ garantuees my jobList_ is not empty, when I acquire the Sempahore in popJob... Thanks for your help. [Edited by - Sparhawk42 on September 16, 2005 8:00:07 AM]