当前位置:网站首页>3. Practice the Thread

3. Practice the Thread

2022-08-09 09:33:00 come here my bear

Thread, netmap download

package com.xiancheng.demo;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.URL;// Practice Thread to achieve multi-threaded synchronous download of pictures// inherit Thread class// Override the run() method// run the start() methodpublic class TestThread2 extends Thread{private String url; // network image addressprivate String name; // saved file addresspublic TestThread2(String url, String name){this.url = url;this.name = name;}public TestThread2() {}// Download the executor of the image [email protected] void run() {WebDownloader webDownloader = new WebDownloader();webDownloader.downloader(url, name);System.out.println("The downloaded file name is: " + name);}public static void main(String[] args) {TestThread2 testThread2 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","2.jpg");TestThread2 testThread3 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","3.jpg");TestThread2 testThread4 = new TestThread2("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","4.jpg");testThread2.start();testThread3.start();testThread4.start();}}class WebDownloader{// download methodpublic void downloader(String url, String name){try {FileUtils.copyURLToFile(new URL(url),new File(name));} catch (IOException e) {e.printStackTrace();System.out.println("IO exception, there is a problem with the downloader method");}}}
原网站

版权声明
本文为[come here my bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090923101861.html