3

以下示例代码 (SSCCE) 抱怨局部变量a必须是最终的。

public class Foo {

    final List<A> list = new ArrayList() {{ add(new A()); }};

    void foo() {
        A a;
        Thread t = new Thread(new Runnable() {
            public void run() {
                a = list.get(0); // not good !
            }
        });
        t.start();
        t.join(0);
        System.out.println(a);
    }

    class A {}
}

为了使事情正常进行,我将代码更改为那个

public class Foo {

    final List<A> list = new ArrayList() {{ add(new A()); }};

    void foo() {

        // A a;
        final ObjectRef x = new ObjectRef();
        Thread t = new Thread(new Runnable() {

            public void run() {
                // a = list.get(0);
                x.set(list.get(0));
            }

        });
        t.start();
        t.join(0);

        // System.out.println(a);
        System.out.println(x.get());
    }

    class A {}

     class ObjectRef<T> {
        T x;

        public ObjectRef() {}

        public ObjectRef(T x) { this.x = x; }

        public void set(T x) {  this.x = x; }

        public T get() { return x; }
    }
}

我的问题:

  1. 这有什么问题吗?
  2. ObjectRef 类作为 JSE 中的标准类存在?
  3. 什么是正确的方法?
4

3 回答 3

4

正确的方法是使用 FutureTask 和 Callable

FutureTask task = new FutureTask(new Callable<A>() {
   public A call() {
      return list.get(0);
   }
});

Executor ex = Executors.newFixedThreadPool(1);
ex.execute(task);

// do something else, code is executing in different thread

A a = task.get(); //get result of execution, will wait if it's not finished yet


ex.shutdown();
于 2010-02-19T10:16:40.097 回答
3

你有没有考虑Callable改用?Callable可以在您产生结果时使用,这似乎是您的情况。

   final List<A> list = new ArrayList() {{ add(new A()); }};

   void foo() {

      Callable<A> call = new Callable<A> {
          A call() throws Exception
          {
              // do something with the list
              return list.get(0);
          }
       }

       ExecutorService executor = new ScheduledThreadPoolExecutor(1);
       Future<A> future = executor.submit(call);

       System.out.println( future.get() );
    }
于 2010-02-19T10:13:59.287 回答
2

我同意你应该使用 Callable 和 FutureTask。

但是可能没有必要使用 Executor:如果您不打算与其他代码共享该 Executor,创建它所需的三行代码,提交任务,然后再次关闭它,看起来太冗长了。你可以只使用一个线程。

FutureTask<A> task = new FutureTask(new Callable<A>() {
   public A call() {
      return list.get(0);
   }
});
new Thread(task).start();
A result = task.get();
于 2010-02-19T10:49:13.210 回答