Example usage for java.util.concurrent FutureTask run

List of usage examples for java.util.concurrent FutureTask run

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask run.

Prototype

public void run() 

Source Link

Usage

From source file:Main.java

/**
 * Run the supplied FutureTask on the main thread. The method will block only if the current
 * thread is the main thread./*  w  ww .  j a v a  2  s.  com*/
 *
 * @param task The FutureTask to run
 * @return The queried task (to aid inline construction)
 */
public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) {
    if (runningOnUiThread()) {
        task.run();
    } else {
        postOnUiThread(task);
    }
    return task;
}

From source file:org.jactr.core.module.AbstractModule.java

/**
 * create a future task and execute it on the exector
 * /*from w ww . ja v  a  2s.  c o m*/
 * @param <T>
 * @param caller
 * @param executor
 * @return
 */
static public <T> Future<T> delayedFuture(Callable<T> caller, Executor executor) {
    FutureTask<T> future = new FutureTask<T>(caller);
    if (executor != null)
        executor.execute(future);
    else
        future.run();

    return future;
}

From source file:org.jactr.core.module.AbstractModule.java

static public <T> Future<T> immediateReturn(T value) {
    FutureTask<T> ft = new FutureTask<T>(new Runnable() {
        public void run() {
            // noop
        }// ww w . j a  va2 s  . c om
    }, value);
    ft.run();
    return ft;
}

From source file:cc.gospy.core.util.StringHelper.java

public static String getMyExternalIp() {
    if (myExternalIp == null) {
        try {//  w ww.  jav a 2s.  c  om
            logger.info("Querying external ip...");
            FutureTask futureTask = new FutureTask<>(() -> {
                URL ipEcho = new URL("http://ipecho.net/plain");
                BufferedReader in = new BufferedReader(new InputStreamReader(ipEcho.openStream()));
                String resultIp = in.readLine();
                in.close();
                return resultIp;
            });
            futureTask.run();
            myExternalIp = (String) futureTask.get(3, TimeUnit.SECONDS);
            logger.info("My external ip: {}", myExternalIp);
        } catch (TimeoutException e) {
            myExternalIp = "unknown ip";
            logger.error("Get external ip failure, cause: Timeout (3 seconds)");
        } catch (Exception e) {
            myExternalIp = "unknown ip";
            logger.error("Get external ip failure, cause: {}", e.getMessage());
        }
    }
    return myExternalIp;
}

From source file:com.googlecode.aviator.AviatorEvaluator.java

/**
 * Compile a text expression to Expression object
 *
 * @param expression//w ww.  jav  a  2  s  .  co  m
 *            text expression
 * @param cached
 *            Whether to cache the compiled result,make true to cache it.
 * @return
 */
public static Expression compile(final String expression, boolean cached) {
    if (expression == null || expression.trim().length() == 0)
        throw new CompileExpressionErrorException("Blank expression");

    if (cached) {
        FutureTask<Expression> task = cacheExpressions.get(expression);
        if (task != null)
            return getCompiledExpression(expression, task);
        task = new FutureTask<Expression>(new Callable<Expression>() {
            @Override
            public Expression call() throws Exception {
                return innerCompile(expression);
            }

        });
        FutureTask<Expression> existedTask = cacheExpressions.putIfAbsent(expression, task);
        if (existedTask == null) {
            existedTask = task;
            existedTask.run();
        }
        return getCompiledExpression(expression, existedTask);

    } else
        return innerCompile(expression);

}

From source file:net.minecraftforge.fml.common.FMLCommonHandler.java

public static void callFuture(FutureTask task) {
    try {/*from w  w  w. j a  v a2 s  . c o m*/
        task.run();
        task.get(); // Forces the exception to be thrown if any
    } catch (InterruptedException e) {
        FMLLog.log(Level.FATAL, e, "Exception caught executing FutureTask: " + e.toString());
    } catch (ExecutionException e) {
        FMLLog.log(Level.FATAL, e, "Exception caught executing FutureTask: " + e.toString());
    }
}

From source file:com.google.code.jconfig.factory.ConfigurationPluginFactory.java

/**
 * <p>/* www  .j  a v  a  2  s .c  o m*/
 *    Returns a plugin instance of type <em>classname</em>.
 * </p>
 * 
 * <p>
 *    If the implementation class use the {@link Cacheable} annotation, it
 *    will be cached for future reuse otherwise a new instance will be
 *    created at every request.
 * </p>
 * 
 * @param classname the full name of an instance of
 *                  {@link IConfigurationPlugin}.
 * @return the plugin instance.
 * @throws PluginInstantiationException
 */
public static IConfigurationPlugin<?> getPlugin(final String classname) throws PluginInstantiationException {

    try {
        if (ClassUtils.getClass(classname).getAnnotation(Cacheable.class) == null) {
            logger.debug("Plugin <" + classname + "> is not cacheable. Creating a new one.");
            return (IConfigurationPlugin<?>) Class.forName(classname).newInstance();
        }
    } catch (Exception e) {
        throw new PluginInstantiationException(e.getMessage(), e);
    }

    // cacheable plugin
    FutureTask<IConfigurationPlugin<?>> theTask = cache.get(classname);
    if (theTask == null) {
        logger.debug("No plugin of class <" + classname + "> available. Creatine a new one.");
        Callable<IConfigurationPlugin<?>> eval = new Callable<IConfigurationPlugin<?>>() {

            public IConfigurationPlugin<?> call() throws Exception {
                IConfigurationPlugin<?> plugin = (IConfigurationPlugin<?>) Class.forName(classname)
                        .newInstance();
                return plugin;
            }
        };

        FutureTask<IConfigurationPlugin<?>> thePluginExec = new FutureTask<IConfigurationPlugin<?>>(eval);
        theTask = cache.putIfAbsent(classname, thePluginExec);
        if (theTask == null) {
            theTask = thePluginExec;
            theTask.run();
        }
        logger.debug("New plugin of class <" + classname + "> ready to be used and cached.");
    } else {
        logger.debug("Plugin of class <" + classname + "> available in cache. Using the cached one.");
    }

    try {
        return theTask.get();
    } catch (Exception e) {
        throw new PluginInstantiationException(e.getMessage(), e);
    } finally {
        logger.debug("plugin cache size: " + cache.size() + " - cache detail: " + cache);
    }
}

From source file:net.dryuf.concurrent.benchmark.NoListenerBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//from w w  w . j  a  v a 2 s  . co  m
@Measurement(iterations = 2, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    for (long i = 0; i < COUNT; ++i) {
        FutureTask<Integer> future = new FutureTask<Integer>(() -> {
            return 0;
        });
        future.run();
    }
}

From source file:net.dryuf.concurrent.benchmark.SinglePostListenerBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//from ww  w .j  av a2 s  . c  o m
@Measurement(iterations = 1, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    for (long i = 0; i < COUNT; ++i) {
        FutureTask<Integer> future = new FutureTask<Integer>(() -> {
            return 0;
        });
        future.run();
    }
}

From source file:net.dryuf.concurrent.benchmark.DoublePreListenerBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//w w w . j  av  a2s.c  om
@Measurement(iterations = 1, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    for (long i = 0; i < COUNT; ++i) {
        FutureTask<Integer> future = new FutureTask<Integer>(() -> {
            return 0;
        });
        future.run();
        future.get();
    }
}