欢迎光临散文网 会员登陆 & 注册

PCI和PCle子系统\/--》ccys1473

2023-07-10 12:27 作者:拆字成诗  | 我要投稿

**

 * 实现Callable接口

 */

public class Demo6_CreateCallable implements Callable<Boolean> {


    private String url;//网络图片地址

    private String name;//报错扥文件名


    //有参构造

    public Demo6_CreateCallable(String url, String name) {

        this.url = url;

        this.name = name;

    }


    //下载图片线程的执行体

    public 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 {

        Demo6_CreateCallable c = new Demo6_CreateCallable("https://img-home.csdnimg.cn/images/20201124032511.png", "1.png");

        Demo6_CreateCallable c1 = new Demo6_CreateCallable("https://img-home.csdnimg.cn/images/20201124032511.png", "2.png");

        Demo6_CreateCallable c2 = new Demo6_CreateCallable("https://img-home.csdnimg.cn/images/20201124032511.png", "3.png");

        //创建执行服务

        ExecutorService ser = Executors.newFixedThreadPool(3);

        //提交执行

        Future<Boolean> r = ser.submit(c);

        Future<Boolean> r1 = ser.submit(c1);

        Future<Boolean> r2 = ser.submit(c2);

        //获取结果

        boolean res = r.get();

        boolean res1 = r1.get();

        boolean res2 = r2.get();

        //关闭服务

        ser.shutdownNow();

    }

}



PCI和PCle子系统\/--》ccys1473的评论 (共 条)

分享到微博请遵守国家法律