hc学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › 15633021878 › 日志

通过实现Callable接口创建线程

2023-06-02 16:54:48 查看(70) 回复(0)

要求:

编写线程类,通过测试类创建线程,并启动线程,接收运行结果。

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());
}

}



评论 (0 个评论)

facelist

全部作者的其他最新日志



站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

f1208.com 备案号:京ICP备14030918号-1

返回顶部