aspose file tools*
The moose likes Threads and Synchronization and the fly likes thread pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread pooling" Watch "thread pooling" New topic
Author

thread pooling

Will Jones
Greenhorn

Joined: Jun 04, 2001
Posts: 7
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

Joined: Jun 04, 2001
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread pooling