List of usage examples for java.util.concurrent Callable call
V call() throws Exception;
From source file:Main.java
/** * Execute a callable with a clean thread context and security context as * to avoid any passing on of thread context and security context to another * thread that may be spawned from here and may end up holding copies in the end. * Any exception thrown will be forwarded via a generic runtime exception. * /*from ww w . j a v a 2 s . c om*/ * @param contextLoader A class loader to set as context class loader * @param callable A {@link Callable} to invoke * @return Return value from the {@link Callable} invocation */ public static <T> T cleanContextExecute(ClassLoader contextLoader, final Callable<T> callable) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(contextLoader); try { return AccessController.doPrivileged(new PrivilegedAction<T>() { public T run() { try { return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } } }); } finally { Thread.currentThread().setContextClassLoader(cl); } }
From source file:org.droidparts.inner.PersistUtils.java
public static <Result> Result executeInTransaction(SQLiteDatabase db, Callable<Result> task) { db.beginTransaction();/* ww w . java 2 s. co m*/ try { Result result = task.call(); db.setTransactionSuccessful(); return result; } catch (Exception e) { L.w(e.getMessage()); L.d(e); return null; } finally { db.endTransaction(); } }
From source file:Lazy.java
/** * Wraps the specified callable with a {@link Lazy} instance. This is an alternative to extending this class. * * @param initializer The initializer. Calling {@link #from} with {@literal null} for an argument will resolve here, * so specifying a {@literal null} initializer is the same as specifying a callable which returns * {@literal null}./*w w w.ja v a2 s .c o m*/ * @return A {@link Lazy} instance for the specified initializer. */ @SuppressWarnings({ "unchecked" }) public static <T> Lazy<T> from(final Callable<T> initializer) { return new Lazy<T>() { @Override public T call() throws Exception { // Lazy.from( null ) will resolve here, so (as a convenience) we support // return of null values return initializer == null ? null : initializer.call(); } }; }
From source file:Main.java
/** * Stupid... but there is places all over that hold on to * AccessControlContexts and Context Class Loaders. This easily consitutes a * leak. So, for stuff that doesn't need anything from the context class * loader this is a way of keeping context to a minimum; * /* w w w. j a v a 2 s . c o m*/ * Execute a callable with a clean thread context and security context as * to avoid any passing on of thread context and security context to another * thread that may be spawned from here and may end up holding copies in the end. * Any exception thrown will be propagated. * * @param contextLoader A class loader to set as context class loader * @param callable A {@link Callable} to invoke * @return Return value from the {@link Callable} invocation * @throws Exception */ public static <T> T cleanContextExceptionExecute(ClassLoader contextLoader, final Callable<T> callable) throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(contextLoader); try { return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() { public T run() throws Exception { return callable.call(); } }); } finally { Thread.currentThread().setContextClassLoader(cl); } }
From source file:com.google.inject.PerformanceComparison.java
static void iterate(Callable<Foo> callable, String label) { int count = 100000; long time = System.currentTimeMillis(); for (int i = 0; i < count; i++) { try {/*w w w . j a va 2s . co m*/ callable.call(); } catch (Exception e) { throw new RuntimeException(e); } } time = System.currentTimeMillis() - time; System.err.println(label + format.format(count * 1000 / time) + " creations/s"); }
From source file:org.apache.hadoop.hbase.TestNamespace.java
private static <V, E> void runWithExpectedException(Callable<V> callable, Class<E> exceptionClass) { try {// ww w.jav a 2 s . c o m callable.call(); } catch (Exception ex) { Assert.assertEquals(exceptionClass, ex.getClass()); return; } fail("Should have thrown exception " + exceptionClass); }
From source file:org.seedstack.seed.cli.SeedRunner.java
/** * Method to execute a callable as a CLI application. * * @param args the command line arguments. * @param callable the callable to execute * @return the return code of the callable * @throws Exception when the CLI command fails to complete. *///from ww w .j a v a2 s . c om public static int execute(String[] args, Callable<Integer> callable) throws Exception { Kernel kernel = startKernel(new CliContext(args)); try { kernel.objectGraph().as(Injector.class).injectMembers(callable); return callable.call(); } finally { stopKernel(kernel); } }
From source file:ee.ria.xroad.asyncdb.AsyncDBUtil.java
/** * Performs task under a lock which applies both to different operations and * different threads.// w w w . j a v a 2 s . com * * @param task - action to be performed * @param lockFilePath - path to file used for process locking * @param lockable - object to be synchronized between threads * @param <T> - type of result * @return - object returned as a result of operation * @throws Exception - when locked operation fails or cannot be performed */ public static <T> T performLocked(Callable<T> task, String lockFilePath, Object lockable) throws Exception { try (RandomAccessFile raf = new RandomAccessFile(lockFilePath, "rw"); FileOutputStream fos = new FileOutputStream(raf.getFD())) { synchronized (lockable) { FileLock lock = fos.getChannel().lock(); try { return task.call(); } finally { lock.release(); } } } }
From source file:io.fabric8.collector.git.GitBuildConfigProcessor.java
/** * A helper method to handle REST APIs which throw a 404 by just returning null *///from ww w. ja va 2 s . c o m public static <T> T handle404ByReturningNull(Callable<T> callable) { try { return callable.call(); } catch (WebApplicationException e) { if (e.getResponse().getStatus() == 404) { return null; } else { throw e; } } catch (Exception e) { throw new WebApplicationException(e); } }
From source file:com.sf.ddao.TxHelper.java
public static <T, SK> T execInTx(TransactionableDao<SK> dao, Callable<T> callable, SK... shardKeys) throws Exception { Context context = dao.startTransaction(shardKeys == null || shardKeys.length == 0 ? null : shardKeys[0]); boolean success = false; Connection conn;/*www .j av a 2s . co m*/ try { conn = ConnectionHandlerHelper.getConnectionOnHold(context); connectionOnHold.set(conn); conn.setAutoCommit(false); T res = callable.call(); conn.commit(); success = true; return res; } finally { connectionOnHold.remove(); conn = ConnectionHandlerHelper.releaseConnectionOnHold(context); if (!success) { conn.rollback(); } conn.close(); } }