package com.yang.java;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ArrayTest {
public static void main(String[] args) {
Window3 td=new Window3();
FutureTask ft=new FutureTask(td);
new Thread(ft).start();
try {
Object sum=ft.get();
System.out.println(sum);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Window3 implements Callable {
@Override
public Object call() throws Exception {
int sum=0;
// TODO Auto-generated method stub
for (int i = 1; i <=100; i++) {
if (i%2==0) {
System.out.println(i);
sum+=i;
}
}
return sum;
}
}
创建多线程方式三实现Callable接口
最新推荐文章于 2023-02-06 23:16:40 发布