Future 是一个接口,看源码有Future 和 FutreTask 使用Demo

package java.util.concurrent;

/**
 * A <tt>Future</tt> represents the result of an asynchronous
 * computation.  Methods are provided to check if the computation is
 * complete, to wait for its completion, and to retrieve the result of
 * the computation.  The result can only be retrieved using method
 * <tt>get</tt> when the computation has completed, blocking if
 * necessary until it is ready.  Cancellation is performed by the
 * <tt>cancel</tt> method.  Additional methods are provided to
 * determine if the task completed normally or was cancelled. Once a
 * computation has completed, the computation cannot be cancelled.
 * If you would like to use a <tt>Future</tt> for the sake
 * of cancellability but not provide a usable result, you can
 * declare types of the form {@code Future<?>} and
 * return <tt>null</tt> as a result of the underlying task.
 *
 * <p>
 * <b>Sample Usage</b> (Note that the following classes are all
 * made-up.) <p>
 *  <pre> {@code
 * interface ArchiveSearcher { String search(String target); }
 * class App {
 *   ExecutorService executor = ...
 *   ArchiveSearcher searcher = ...
 *   void showSearch(final String target)
 *       throws InterruptedException {
 *     Future<String> future
 *       = executor.submit(new Callable<String>() {
 *         public String call() {
 *             return searcher.search(target);
 *         }});
 *     displayOtherThings(); // do other things while searching
 *     try {
 *        displayText(future.get()); // use future
 *     } catch (ExecutionException ex) { cleanup(); return; }
 *   }
 * }}</pre>
 *
 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
 * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
 * For example, the above construction with <tt>submit</tt> could be replaced by:
 *  <pre> {@code
 *     FutureTask<String> future =
 *       new FutureTask<String>(new Callable<String>() {
 *         public String call() {
 *           return searcher.search(target);
 *       }});
 *     executor.execute(future);}</pre>
 *
 * <p>Memory consistency effects: Actions taken by the asynchronous computation
 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
 * actions following the corresponding {@code Future.get()} in another thread.
 *
 * @see FutureTask
 * @see Executor
 * @since 1.5
 * @author Doug Lea
 * @param <V> The result type returned by this Future's <tt>get</tt> method
 */
public interface Future<V> {

 

 1:测试类 App.

package com.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class App {
    static ExecutorService executor = null;
    static ArchiveSearcher searcher = null;

    public static void main(String[] args) {
        executor = Executors.newSingleThreadScheduledExecutor();
        searcher = new ArchiveSearcherImp();

        try {
            showSearch("hello");
            //showSearch2("world");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    static void showSearch(final String target) throws InterruptedException {
        Future<String> future = executor.submit(new Callable<String>() {
            public String call() {
                return searcher.search(target);
            }
        });
        System.out.println("displayOtherThings");
        //boolean c = future.cancel(true);
        // do other things while searching
        try {
            System.out.println("displayText(" + future.get() + ")");
        } catch (Exception ex) {
            System.out.println("Future Canceled:" + future.isCancelled());
            return;
        }
    }

    static void showSearch2(final String target) throws InterruptedException {
        FutureTask<String> future = new FutureTask<String>(
                new Callable<String>() {
                    public String call() {
                        return searcher.search(target);
                    }
                });
        executor.execute(future);
    }

}
View Code

相关文章:

  • 2022-12-23
  • 2021-10-06
  • 2021-08-09
  • 2021-11-17
  • 2021-05-25
猜你喜欢
  • 2021-11-22
  • 2021-09-17
  • 2021-06-09
  • 2021-12-17
  • 2022-01-24
相关资源
相似解决方案