Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.util;
     18 
     19 import java.util.concurrent.Callable;
     20 
     21 // NOTE: If the Callable throws any Throwable, the result value will be null.
     22 public class FutureTask<T> implements Runnable, Future<T> {
     23     private static final String TAG = "FutureTask";
     24     private Callable<T> mCallable;
     25     private FutureListener<T> mListener;
     26     private volatile boolean mIsCancelled;
     27     private boolean mIsDone;
     28     private T mResult;
     29 
     30     public FutureTask(Callable<T> callable, FutureListener<T> listener) {
     31         mCallable = callable;
     32         mListener = listener;
     33     }
     34 
     35     public FutureTask(Callable<T> callable) {
     36         this(callable, null);
     37     }
     38 
     39     public void cancel() {
     40         mIsCancelled = true;
     41     }
     42 
     43     public synchronized T get() {
     44         while (!mIsDone) {
     45             try {
     46                 wait();
     47             } catch (InterruptedException t) {
     48                 // ignore.
     49             }
     50         }
     51         return mResult;
     52     }
     53 
     54     public void waitDone() {
     55         get();
     56     }
     57 
     58     public synchronized boolean isDone() {
     59         return mIsDone;
     60     }
     61 
     62     public boolean isCancelled() {
     63         return mIsCancelled;
     64     }
     65 
     66     public void run() {
     67         T result = null;
     68 
     69         if (!mIsCancelled) {
     70             try {
     71                 result = mCallable.call();
     72             } catch (Throwable ex) {
     73                 Log.w(TAG, "Exception in running a task", ex);
     74             }
     75         }
     76 
     77         synchronized(this) {
     78             mResult = result;
     79             mIsDone = true;
     80             if (mListener != null) {
     81                 mListener.onFutureDone(this);
     82             }
     83             notifyAll();
     84         }
     85     }
     86 }
     87