了解未来/线程

时间:2010-05-12 16:04:40

标签: java multithreading future

我试图第一次使用期货。你可以取消一份工作似乎很聪明,但它没有按预期工作。在下面的示例中,仅取消第一个作业。其余的都完成了。我误解了期货的使用吗?

public class ThreadExample 
{
    public static void main(String[] args) throws InterruptedException, ExecutionException 
    {
        int processors = Runtime.getRuntime().availableProcessors();
        System.out.println("Processors: " + processors);
        ExecutorService es = Executors.newFixedThreadPool(processors);
        int nowork = 10;
        Future<Integer>[] workres = new Future[nowork];
        for(int i = 0; i < nowork; i++)
        {
            workres[i] = es.submit(new SomeWork(i));
        }
        for(int i = 0; i < nowork; i++) 
        {
            if(i % 2 == 0)
            {
                System.out.println("Cancel");
                workres[i].cancel(true);
            }
            if(workres[i].isCancelled())
            {
                System.out.println(workres[i] + " is cancelled");
            }
            else
            {
                System.out.println(workres[i].get());
            }
        }
        es.shutdown();
    }
}

class SomeWork implements Callable<Integer> 
{
    private int v;
    public SomeWork(int v) 
    {
        this.v = v;
    }

    @Override
    public Integer call() throws Exception
    {
        TimeUnit.SECONDS.sleep(5);
        System.out.println(v + " done at " + (new Date()));
        return v;
    }
}

输出:

Processors: 4
Cancel
java.util.concurrent.FutureTask@10d448 is cancelled
4 done at Wed May 12 17:47:05 CEST 2010
2 done at Wed May 12 17:47:05 CEST 2010
1 done at Wed May 12 17:47:05 CEST 2010
3 done at Wed May 12 17:47:05 CEST 2010
1
Cancel
2  
3
Cancel
4
5 done at Wed May 12 17:47:10 CEST 2010
7 done at Wed May 12 17:47:10 CEST 2010
8 done at Wed May 12 17:47:10 CEST 2010
6 done at Wed May 12 17:47:10 CEST 2010  
5
Cancel
6
7
Cancel
8
9 done at Wed May 12 17:47:15 CEST 2010  
9

2 个答案:

答案 0 :(得分:8)

Future#cancel()不会终止/中断已在运行的作业。它只会取消尚未运行的工作。

更新:polygenelubricants将根本原因缩小(+1):这是改进的代码:

int processors = Runtime.getRuntime().availableProcessors();
System.out.println("Processors: " + processors);
ExecutorService es = Executors.newFixedThreadPool(processors);
int nowork = 10;
Future<Integer>[] workers = new Future[nowork];

for (int i = 0; i < nowork; i++) {
    final int ii = i;
    workers[i] = es.submit(new Callable<Integer>() {
        public Integer call() throws Exception {
            return ii;
        }
    });
}

for (int i = 0; i < nowork; i++) {
    if (i % 2 == 0) {
        System.out.println("Cancel worker " + i);
        workers[i].cancel(true);
    }
}

for (int i = 0; i < nowork; i++) {
    if (workers[i].isCancelled()) {
        System.out.println("Worker " + i + " is cancelled");
    } else {
        System.out.println("Worker " + i + " returned: " + workers[i].get());
    }
}

es.shutdown();

结果:

Processors: 2
Cancel worker 0
Cancel worker 2
Cancel worker 4
Cancel worker 6
Cancel worker 8
Worker 0 is cancelled
Worker 1 returned: 1
Worker 2 is cancelled
Worker 3 returned: 3
Worker 4 is cancelled
Worker 5 returned: 5
Worker 6 is cancelled
Worker 7 returned: 7
Worker 8 is cancelled
Worker 9 returned: 9

(请注意,它是workers,而不是workres。)

答案 1 :(得分:8)

问题是你的取消循环与你的get()循环重叠,这会阻塞。我想你想要2个循环,不是吗?一个循环取消偶数编号的作业,然后是第二个循环,检查哪些取消,哪些取消,然后相应地get()

现在的方式,在循环甚至有机会取消workres[2]之前,它已经检查并从get()询问workres[1]

所以我认为你需要3个阶段:

1. The `submit()` loop
2. The selective `cancel()` loop
3. The selective `get()` loop (which blocks)