创建线程的方法2

实现Runnable类

package com.peanutist.day06;

import org.apache.commons.io.FileUtils;

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

public class TestThread02 implements Runnable{
   String url;
   String file;

   public TestThread02(String url, String file) {
       this.url = url;
       this.file = file;
  }

   public void run(){
       new PicDownLoader().downLoader(this.url,this.file);
       System.out.println(this.file+"下载ok了");
  }

   public static void main(String[] args) {
       TestThread02 file04 = new TestThread02("https://img2.doubanio.com/view/dale-online/dale_ad/public/bdc4648bae1f2ff.jpg", "file04");
       TestThread02 file05 = new TestThread02("https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2630627669.webp", "file05");
       TestThread02 file06 = new TestThread02("https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2629056068.webp", "file06");

       new Thread(file04).start();
       new Thread(file05).start();
       new Thread(file06).start();

  }
}
class PicDownLoader{
   public void downLoader(String url,String file){
       try {
           FileUtils.copyURLToFile(new URL(url),new File(file));
      } catch (IOException e) {
           e.printStackTrace();
      }
  }
}
posted on 2021-03-04 17:43  要给小八赚罐头钱  阅读(112)  评论(0)    收藏  举报