• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread pooling

 
Will Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to code in the java code listed at
this site... http://www.sys-con.com/java/source/3-2/34.htm
I wrote my own ClientHandler... Basically,
it's just a knockknockjoke server. Anyway,
I'm having problems getting it to work correctly
and am unsure of why or how to debug it.
One client can connect okay, but when I try a second
one it hangs waiting to enter the synchronized ThreadPoolManager.start method.
As far as I know it would seem that I could only be
locked out like this if some other thread had a
lock on this code. But who has a lock on it? It
should be free at this point. I'm confused and
would appreciate any help.
 
Will Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I figured it out. I'll explain how I fixed it in case
someone cares...
Here were the original threadWaiting and start methods on the threadpool manager class...
synchronized void threadWaiting( ManagedThread managedThread ) throws InterruptedException
{
Runnable runnable = null;
while( m_runnableVector.size() == 0 )
{
wait();
}
runnable = (Runnable) m_runnableVector.elementAt(0);
m_runnableVector.removeElementAt(0);
managedThread.startRunnable( runnable );
}
public synchronized void start(Runnable r)
{
m_runnableVector.addElement(r);
//Tell the thread manager we just added a runnable thread...
notify();
}
When managedThread.startRunnable( runnable ) was called in
the threadWaiting method it started the ClientHandler thread.
My ClientHandler thread had I/O in it from the user. While
I was in my ClientHandler code the lock on threadWaiting was
kept. Only after it returned from the ClientHandler would
it release the lock. In order to correct the problem I moved
the synchronized keyword in a bit...
void threadWaiting( ManagedThread managedThread ) throws InterruptedException
{
Runnable runnable = null;
synchronized( this )
{
while( m_runnableVector.size() == 0 )
{
wait();
}
runnable = (Runnable) m_runnableVector.elementAt(0);
m_runnableVector.removeElementAt(0);
}
managedThread.startRunnable( runnable );
}
This fixed the problem, by releasing the lock before calling
the managedThread.startRunnable method. It allowed the synchronized start method to be entered by another thread
while another ClientHandler was running.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!