多线程DEMO实现简单功能

 1.Runnable实现根据URL 下载图片

依赖jar包

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.7</version>
</dependency>
package com.kuang.test;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Auther: 18352
 * @Date: 2021/7/25 17:51
 * @Description: TODO
 */
public class TestTheead implements   Runnable{
    private String url;
    private String name;

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

    @Override
    public void run() {
        DownFile downFile = new DownFile();
        downFile.cocyurl(url, name);
        System.out.println("下载完成"+name);
    }

    public static void main(String[] args) {
        TestTheead testTheead = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
        TestTheead testTheead2 = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
        TestTheead testTheead3 = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
        Thread thread1 = new Thread(testTheead);
        Thread thread2 = new Thread(testTheead2);
        Thread thread3 = new Thread(testTheead3);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class DownFile{

    public void cocyurl(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值