实现Callable

Posted nihaoya-czw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现Callable相关的知识,希望对你有一定的参考价值。

1.创建目标对象

2.创建执行服务

3.提交执行

4.获取结果

5.关闭服务

具体如下:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CDownloader implements Callable<Boolean>{
private String url; //远程路径
private String name; //存储名字

public CDownloader(String url, String name) {
this.url = url; 
this.name = name;
}

@Override
public Boolean call() throws Exception {
WebDownloader wd =new WebDownloader();
wd.download(url, name);    
System.out.println(name);
return true;
}

public static void main(String[] args) throws InterruptedException, ExecutionException {
CDownloader cd1 =new CDownloader("http://upload.news.cecb2b.com/2014/0511/1399775432250.jpg","phone.jpg");
CDownloader cd2 =new CDownloader("http://p1.pstatp.com/large/403c00037462ae2eee13","spl.jpg");
CDownloader cd3 =new CDownloader("http://5b0988e595225.cdn.sohucs.com/images/20170830/d8b57e0dce0d4fa29bd5ef014be663d5.jpeg","success.jpg");

//创建执行服务: 
ExecutorService ser=Executors.newFixedThreadPool(3);
//提交执行: 
Future<Boolean> result1 =ser.submit(cd1) ;
Future<Boolean> result2 =ser.submit(cd2) ;
Future<Boolean> result3 =ser.submit(cd3) ;
//获取结果: 
boolean r1 =result1.get();
boolean r2 =result1.get();
boolean r3 =result1.get();
System.out.println(r3);
//关闭服务: 
ser.shutdownNow();

}
}

 

以上是关于实现Callable的主要内容,如果未能解决你的问题,请参考以下文章

Java Callable接口——有返回值的线程

java学习之Runnable, Callable, Future, FutureTask

创建线程之三:实现Callable接口

实现Callable接口,并与Future结合使用

十获取异步线程返回值Callable

java多线程-Executors实现的几种线程池以及Callable