use of java.util.concurrent.FutureTask in project spring-framework by spring-projects.
the class TaskExecutorAdapter method submit.
@Override
public <T> Future<T> submit(Callable<T> task) {
try {
if (this.taskDecorator == null && this.concurrentExecutor instanceof ExecutorService) {
return ((ExecutorService) this.concurrentExecutor).submit(task);
} else {
FutureTask<T> future = new FutureTask<>(task);
doExecute(this.concurrentExecutor, this.taskDecorator, future);
return future;
}
} catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
}
}
use of java.util.concurrent.FutureTask in project platform_frameworks_base by android.
the class MtpManagerTest method testCancelEvent.
public void testCancelEvent() throws Exception {
final CancellationSignal signal = new CancellationSignal();
final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
@Override
public Boolean call() throws IOException {
try {
while (true) {
mManager.readEvent(mUsbDevice.getDeviceId(), signal);
}
} catch (OperationCanceledException exception) {
return true;
}
}
});
final Thread thread = new Thread(future);
thread.start();
SystemClock.sleep(TIMEOUT_MS);
signal.cancel();
assertTrue(future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
}
use of java.util.concurrent.FutureTask in project platform_frameworks_base by android.
the class MffTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
// MffContext needs to be created on a separate thread to allow MFF to post Runnable's.
mMffContextHandlerThread = new HandlerThread("MffContextThread");
mMffContextHandlerThread.start();
Handler handler = new Handler(mMffContextHandlerThread.getLooper());
FutureTask<MffContext> task = new FutureTask<MffContext>(new Callable<MffContext>() {
@Override
public MffContext call() throws Exception {
MffContext.Config config = new MffContext.Config();
config.requireCamera = false;
config.requireOpenGL = false;
config.forceNoGL = true;
return new MffContext(getContext(), config);
}
});
handler.post(task);
// Wait for the context to be created on the handler thread.
mMffContext = task.get();
}
use of java.util.concurrent.FutureTask in project android-async-http by loopj.
the class AsyncBackgroundThreadSample method executeSample.
@Override
public RequestHandle executeSample(final AsyncHttpClient client, final String URL, final Header[] headers, HttpEntity entity, final ResponseHandlerInterface responseHandler) {
final Activity ctx = this;
FutureTask<RequestHandle> future = new FutureTask<>(new Callable<RequestHandle>() {
public RequestHandle call() {
Log.d(LOG_TAG, "Executing GET request on background thread");
return client.get(ctx, URL, headers, null, responseHandler);
}
});
executor.execute(future);
RequestHandle handle = null;
try {
handle = future.get(5, TimeUnit.SECONDS);
Log.d(LOG_TAG, "Background thread for GET request has finished");
} catch (Exception e) {
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return handle;
}
use of java.util.concurrent.FutureTask in project android-async-http by loopj.
the class AsyncBackgroundThreadSample method getResponseHandler.
@Override
public ResponseHandlerInterface getResponseHandler() {
FutureTask<ResponseHandlerInterface> future = new FutureTask<>(new Callable<ResponseHandlerInterface>() {
@Override
public ResponseHandlerInterface call() throws Exception {
Log.d(LOG_TAG, "Creating AsyncHttpResponseHandler on background thread");
return new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onStart() {
clearOutputs();
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
Log.d(LOG_TAG, String.format("onSuccess executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper()));
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
debugResponse(LOG_TAG, new String(response));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
Log.d(LOG_TAG, String.format("onFailure executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper()));
debugHeaders(LOG_TAG, headers);
debugStatusCode(LOG_TAG, statusCode);
debugThrowable(LOG_TAG, e);
if (errorResponse != null) {
debugResponse(LOG_TAG, new String(errorResponse));
}
}
@Override
public void onRetry(int retryNo) {
Toast.makeText(AsyncBackgroundThreadSample.this, String.format("Request is retried, retry no. %d", retryNo), Toast.LENGTH_SHORT).show();
}
};
}
});
executor.execute(future);
ResponseHandlerInterface responseHandler = null;
try {
responseHandler = future.get();
Log.d(LOG_TAG, "Background thread for AsyncHttpResponseHandler has finished");
} catch (Exception e) {
e.printStackTrace();
}
return responseHandler;
}
Aggregations