430654 - closing client connections can hang worker threads.

Prettified usage of NonBlockingThread and added Javadocs.
This commit is contained in:
Simone Bordet 2014-03-27 15:37:23 +01:00
parent 1089a33578
commit 342c97d8ba
2 changed files with 29 additions and 18 deletions

View File

@ -204,7 +204,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
ManagedSelector selector = newSelector(i);
_selectors[i] = selector;
selector.start();
execute(selector);
execute(new NonBlockingThread(selector));
}
}
@ -476,21 +476,14 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
public void run()
{
_thread = Thread.currentThread();
final String name = _thread.getName();
String name = _thread.getName();
try
{
NonBlockingThread.runAsNonBlocking(new Runnable()
{
@Override
public void run()
{
_thread.setName(name + "-selector-" + SelectorManager.this.getClass().getSimpleName()+"@"+Integer.toHexString(SelectorManager.this.hashCode())+"/"+_id);
LOG.debug("Starting {} on {}", _thread, this);
while (isRunning())
select();
runChanges();
}
});
_thread.setName(name + "-selector-" + SelectorManager.this.getClass().getSimpleName()+"@"+Integer.toHexString(SelectorManager.this.hashCode())+"/"+_id);
LOG.debug("Starting {} on {}", _thread, this);
while (isRunning())
select();
runChanges();
}
finally
{

View File

@ -18,20 +18,38 @@
package org.eclipse.jetty.util.thread;
public class NonBlockingThread
/**
* Marker that wraps a Runnable, indicating that it is running in a thread that must not be blocked.
* <p />
* Client code can use the thread-local {@link #isNonBlockingThread()} to detect whether they are
* in the context of a non-blocking thread, and perform different actions if that's the case.
*/
public class NonBlockingThread implements Runnable
{
private final static ThreadLocal<Boolean> __nonBlockingThread = new ThreadLocal<>();
private final static ThreadLocal<Boolean> __nonBlockingThread = new ThreadLocal<>();
/**
* @return whether the current thread is a thread that must not block.
*/
public static boolean isNonBlockingThread()
{
return Boolean.TRUE.equals(__nonBlockingThread.get());
}
public static void runAsNonBlocking(Runnable runnable)
private final Runnable delegate;
public NonBlockingThread(Runnable delegate)
{
this.delegate = delegate;
}
@Override
public void run()
{
try
{
__nonBlockingThread.set(Boolean.TRUE);
runnable.run();
delegate.run();
}
finally
{