java多线程的实现方式
作者:
稀饭粥95 | 来源:发表于
2018-09-01 21:06 被阅读5次继承Callable接口
public class Main {
public static void main(String[] args) {
Callable<Integer> call = new Callable<Integer>(){
public Integer call(){
System.out.println("call");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 10;
}
};
FutureTask<Integer> future = new FutureTask<Integer>(call);
//FutureTask 可用于 闭锁 类似于CountDownLatch的作用,在所有的线程没有执行完成之后这里是不会执行的
Thread th = new Thread(future);
th.start();
try {
int out =future.get();
System.out.println(out);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文标题:java多线程的实现方式
本文链接:https://www.haomeiwen.com/subject/ocauwftx.html
网友评论