Code cleanups.
This commit is contained in:
parent
f695d9cf31
commit
1a88dfa8c0
|
@ -21,15 +21,21 @@ package org.eclipse.jetty.util.thread;
|
|||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* An object (typically either a {@link Runnable} or {@link Callable}
|
||||
* that can declare how it will behaive when invoked: blocking, non-blocking
|
||||
* or either.
|
||||
*
|
||||
* <p>A task (typically either a {@link Runnable} or {@link Callable}
|
||||
* that declares how it will behave when invoked:</p>
|
||||
* <ul>
|
||||
* <li>blocking, the invocation will certainly block (e.g. performs blocking I/O)</li>
|
||||
* <li>non-blocking, the invocation will certainly <strong>not</strong> block</li>
|
||||
* <li>either, the invocation <em>may</em> block</li>
|
||||
* </ul>
|
||||
*/
|
||||
public interface Invocable
|
||||
{
|
||||
enum InvocationType { BLOCKING, NON_BLOCKING, EITHER };
|
||||
|
||||
enum InvocationType
|
||||
{
|
||||
BLOCKING, NON_BLOCKING, EITHER
|
||||
}
|
||||
|
||||
static ThreadLocal<Boolean> __nonBlocking = new ThreadLocal<Boolean>()
|
||||
{
|
||||
@Override
|
||||
|
@ -38,7 +44,7 @@ public interface Invocable
|
|||
return Boolean.FALSE;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public static boolean isNonBlockingInvocation()
|
||||
{
|
||||
return __nonBlocking.get();
|
||||
|
@ -58,34 +64,16 @@ public interface Invocable
|
|||
__nonBlocking.set(was_non_blocking);
|
||||
}
|
||||
}
|
||||
|
||||
public static void invokeOnlyNonBlocking(Runnable task)
|
||||
{
|
||||
switch(getInvocationType(task))
|
||||
{
|
||||
case BLOCKING:
|
||||
throw new IllegalArgumentException("Cannot invoke nonblocking: "+task);
|
||||
|
||||
case NON_BLOCKING:
|
||||
task.run();
|
||||
break;
|
||||
|
||||
case EITHER:
|
||||
// a Choice exists, so we must indicate NonBlocking
|
||||
invokeNonBlocking(task);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void invokePreferNonBlocking(Runnable task)
|
||||
{
|
||||
switch(getInvocationType(task))
|
||||
switch (getInvocationType(task))
|
||||
{
|
||||
case BLOCKING:
|
||||
case NON_BLOCKING:
|
||||
task.run();
|
||||
task.run();
|
||||
break;
|
||||
|
||||
|
||||
case EITHER:
|
||||
// a Choice exists, so we must indicate NonBlocking
|
||||
invokeNonBlocking(task);
|
||||
|
@ -95,66 +83,48 @@ public interface Invocable
|
|||
|
||||
public static void invokePreferred(Runnable task, InvocationType preferredInvocationType)
|
||||
{
|
||||
switch(getInvocationType(task))
|
||||
switch (getInvocationType(task))
|
||||
{
|
||||
case BLOCKING:
|
||||
case NON_BLOCKING:
|
||||
task.run();
|
||||
task.run();
|
||||
break;
|
||||
|
||||
|
||||
case EITHER:
|
||||
if (getInvocationType(task)==InvocationType.EITHER && preferredInvocationType==InvocationType.NON_BLOCKING)
|
||||
if (getInvocationType(task) == InvocationType.EITHER && preferredInvocationType == InvocationType.NON_BLOCKING)
|
||||
invokeNonBlocking(task);
|
||||
else
|
||||
task.run();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Runnable asPreferred(Runnable task, InvocationType preferredInvocationType)
|
||||
{
|
||||
switch(getInvocationType(task))
|
||||
switch (getInvocationType(task))
|
||||
{
|
||||
case BLOCKING:
|
||||
case NON_BLOCKING:
|
||||
break;
|
||||
|
||||
case EITHER:
|
||||
if (getInvocationType(task)==InvocationType.EITHER && preferredInvocationType==InvocationType.NON_BLOCKING)
|
||||
return new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
invokeNonBlocking(task);
|
||||
}
|
||||
};
|
||||
if (getInvocationType(task) == InvocationType.EITHER && preferredInvocationType == InvocationType.NON_BLOCKING)
|
||||
return () -> invokeNonBlocking(task);
|
||||
break;
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
|
||||
public static void invokePreferBlocking(Runnable task)
|
||||
{
|
||||
task.run();
|
||||
}
|
||||
|
||||
|
||||
public static InvocationType getInvocationType(Object o)
|
||||
{
|
||||
if (o instanceof Invocable)
|
||||
return ((Invocable)o).getInvocationType();
|
||||
return InvocationType.BLOCKING;
|
||||
}
|
||||
|
||||
|
||||
default InvocationType getInvocationType()
|
||||
{
|
||||
return InvocationType.BLOCKING;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue