要求:
编写线程类,通过测试类创建线程,并启动线程,接收运行结果。
import org.junit.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class 随便2 implements Callable<Integer> {
int sun=0;
@Override
public Integer call() throws Exception {
for (int i = 0; i <101 ; i++) {
sun+=i;
}
return sun;
}
}
class b{
public static void main(String[] args) throws ExecutionException, InterruptedException {
随便2 a=new 随便2();
FutureTask<Integer> f=new FutureTask<>(a);
Thread s=new Thread(f);
s.start();
System.out.println(f.get());
}
}