de.mklinger.qetcher.client.jetty.util.thread.Invocable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qetcher-client-bundle Show documentation
Show all versions of qetcher-client-bundle Show documentation
Qetcher Java client, OSGi bundle, minimal dependencies
//
// ========================================================================
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import java.util.concurrent.Callable;
/**
* A task (typically either a {@link Runnable} or {@link Callable}
* that declares how it will behave when invoked:
*
* - blocking, the invocation will certainly block (e.g. performs blocking I/O)
* - non-blocking, the invocation will certainly not block
* - either, the invocation may block
*
*
*
* Static methods and are provided that allow the current thread to be tagged
* with a {@link ThreadLocal} to indicate if it has a blocking invocation type.
*
*
*/
public interface Invocable
{
enum InvocationType
{
BLOCKING, NON_BLOCKING, EITHER
}
static ThreadLocal __nonBlocking = new ThreadLocal<>();
/**
* Test if the current thread has been tagged as non blocking
* @return True if the task the current thread is running has
* indicated that it will not block.
*/
public static boolean isNonBlockingInvocation()
{
return Boolean.TRUE.equals(__nonBlocking.get());
}
/**
* Invoke a task with the calling thread, tagged to indicate
* that it will not block.
* @param task The task to invoke.
*/
public static void invokeNonBlocking(Runnable task)
{
Boolean was_non_blocking = __nonBlocking.get();
try
{
__nonBlocking.set(Boolean.TRUE);
task.run();
}
finally
{
__nonBlocking.set(was_non_blocking);
}
}
/**
* Get the invocation type of an Object.
* @param o The object to check the invocation type of.
* @return If the object is an Invocable, it is coerced and the {@link #getInvocationType()}
* used, otherwise {@link InvocationType#BLOCKING} is returned.
*/
public static InvocationType getInvocationType(Object o)
{
if (o instanceof Invocable)
return ((Invocable)o).getInvocationType();
return InvocationType.BLOCKING;
}
/**
* @return The InvocationType of this object
*/
default InvocationType getInvocationType()
{
return InvocationType.BLOCKING;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy