Search in sources :

Example 16 with FutureTask

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);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) FutureTask(java.util.concurrent.FutureTask) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 17 with FutureTask

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));
}
Also used : FutureTask(java.util.concurrent.FutureTask) OperationCanceledException(android.os.OperationCanceledException) IOException(java.io.IOException) CancellationSignal(android.os.CancellationSignal)

Example 18 with FutureTask

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();
}
Also used : HandlerThread(android.os.HandlerThread) FutureTask(java.util.concurrent.FutureTask) Handler(android.os.Handler)

Example 19 with FutureTask

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;
}
Also used : FutureTask(java.util.concurrent.FutureTask) RequestHandle(com.loopj.android.http.RequestHandle) Activity(android.app.Activity)

Example 20 with FutureTask

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;
}
Also used : AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) FutureTask(java.util.concurrent.FutureTask) Header(cz.msebera.android.httpclient.Header) ResponseHandlerInterface(com.loopj.android.http.ResponseHandlerInterface)

Aggregations

FutureTask (java.util.concurrent.FutureTask)111 ExecutionException (java.util.concurrent.ExecutionException)41 IOException (java.io.IOException)26 Test (org.junit.Test)24 Callable (java.util.concurrent.Callable)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 ExecutorService (java.util.concurrent.ExecutorService)18 TimeoutException (java.util.concurrent.TimeoutException)13 Handler (android.os.Handler)12 ArrayList (java.util.ArrayList)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 CancellationException (java.util.concurrent.CancellationException)8 AccessibleObject (java.lang.reflect.AccessibleObject)6 Future (java.util.concurrent.Future)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CancellationSignal (android.os.CancellationSignal)4 OperationCanceledException (android.os.OperationCanceledException)4