实现callable接口(了解即可)
-
实现callable接口,需要返回值类型
-
重写call方法,需要抛出异常
-
创建目标对象
-
创建执行服务:ExecutorService ser=Executor.newFixedThreadPool(1);
-
提交服务
package com.fei.demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
public class TextCallbale implements Callable<Boolean> {
private String url;
private String name;
public TextCallbale(String url,String name){
this.url=url;
this.name=name;
}
@Override
public Boolean call() {
webDownLoader webDownLoader = new webDownLoader();
webDownLoader.downloader(url,name);
System.out.println("下载文件图片名为:"+name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TextCallbale t1=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","1.jpg");
TextCallbale t2=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","2.jpg");
TextCallbale t3=new TextCallbale("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F010421143P0%2F210104143P0-4-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668094322&t=1222fed63d4d2315eb80ed377579ec90","3.jpg");