package com.aflfte.thread;
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;
/**
* 多线程实现的方式三
* 实现Callable接口
* 重写Call方法
* @author root
*
*/
public class CDownloader implements Callable<Boolean> {
private String url;
private String name;
public CDownloader(String url, String name) {
super();
this.url = url;
this.name = name;
}
@Override
public Boolean call() throws Exception {
WebDownloader dw=new WebDownloader();
dw.download(url, name);
System.out.println(name);
return true;
}
public static void main(String[] args) throws Exception, Exception {
CDownloader cd1=new CDownloader("http://www.aflfte.com/aflfte/content/uploadfile/201912/thum-85601575250807.jpg", "lei.jpg");
CDownloader cd2=new CDownloader("http://www.glfhw.com/a/uploads/allimg/181110/1-1Q110141640.jpg", "12.jpg");
CDownloader cd3=new CDownloader("http://www.glfhw.com/a/uploads/allimg/120514/1-1205140949190-L.jpg", "3.jpg");
ExecutorService ser=Executors.newFixedThreadPool(1);
Future<Boolean> result1=ser.submit(cd1);
Future<Boolean> result2=ser.submit(cd2);
Future<Boolean> result3=ser.submit(cd3);
boolean r1=result1.get();
boolean r2=result2.get();
boolean r3=result3.get();
ser.shutdownNow();
}
}
« lambda表达式用法
|
创建线程方法二»
|