多线程继承Thread类

2021/4/8 18:25:16

本文主要是介绍多线程继承Thread类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

多线程继承Thread类

  1. package com.lening.thread;
    
    import lombok.SneakyThrows;
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    /**
     * @Author WangEn
     * @CreateTime: 2021-04-07-11-29
     * @Emile: wen_5988@163.com
     */
    public class TestThread extends Thread {
        private String url;
        private String name;
    
        public TestThread(String url, String name) {
            this.url = url;
            this.name = name;
        }
    
        @SneakyThrows
        @Override
        public void run() {
            new UpDownLoad().upDownLoad(url,name);
            System.out.println("下载的图片名字是"+name);
        }
    
        public static void main(String[] args) {
            new TestThread("https://img1.baidu.com/it/u=1485012388,2380514454&fm=26&fmt=auto&gp=0.jpg","地球.jpg").start();
            new TestThread("https://img2.baidu.com/it/u=3355464299,584008140&fm=26&fmt=auto&gp=0.jpg","A梦.jpg").start();
            new TestThread("https://img1.baidu.com/it/u=2496571732,442429806&fm=26&fmt=auto&gp=0.jpg","水.jpg").start();
        }
    }
    
    
    /**
     * 下图工具类
     */
    class UpDownLoad{
        public void upDownLoad(String url,String name) throws MalformedURLException {
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("图片下载失败");
            }
        }
    }
    

2.FileUtils是api开发下面的一个下载工具包

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>


这篇关于多线程继承Thread类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程