
Callable
重写 public Boolean call()方法
package com.pln.callables;import com.pln.thread.TestThread2;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;import java.util.concurrent.*;public class TestCallable1 implements Callable <Boolean>{private String url;private String name;public TestCallable1(String url,String name){this.url = url;this.name = name;}@Overridepublic Boolean call() throws Exception {WebDownLoader webDownLoader = new WebDownLoader();webDownLoader.downLoader(url,name);System.out.println("下载名字+"+name);return true;}public static void main(String[] args) throws ExecutionException, InterruptedException {// 调用带参的构造方法TestCallable1 t1 = new TestCallable1("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20196302356495401.jpg","1.jpg");TestCallable1 t2 = new TestCallable1("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20196302342173219.jpg","2.jpg");TestCallable1 t3 = new TestCallable1("http://www.mdjdx.cn/mdtpweb/upload/ew/uploadfile/f20198310114727.jpg","3.jpg");//创建执行服务ExecutorService ser = Executors.newFixedThreadPool(3);//3代表有三个对象三张图片下载//提交执行Future<Boolean> r1 = ser.submit(t1);Future<Boolean> r2 = ser.submit(t2);Future<Boolean> r3 = ser.submit(t3);//获取结果boolean rs1 = r1.get();boolean rs2 = r2.get();boolean rs3 = r3.get();//关闭服务ser.shutdownNow();}}//下载器class WebDownLoader {// 下载方法public void downLoader(String url,String name) throws IOException {FileUtils.copyURLToFile(new URL(url),new File(name));}}
