[Java]多线程基础操作/创建线程/下载图片
创建线程的三种方式
- 继承Thread类(重点)
- 实现Runnable接口(重点)推荐使用
- 实现Callable接口(了解)
继承Thread类
package Thread; /** * 创建线程方式一:继承Thread * * @author ZhaoLu cang on 2021/4/10 0010 */ //注意,线程开启不一定立即执行,由cpu调度执行 public class Thread01 extends Thread{ //重写run方法 @Override public void run() { //run方法线程体 for(int i=0;i<20;i++){ System.out.println("学习多线程中......"); } } public static void main(String[] args) throws InterruptedException { //创建一个线程对象 Thread01 thread=new Thread01(); //调用start方法 thread.start(); //main主线程 for(int i=0;i<20;i++){ System.out.println("正在学习代码......"); } } }
多线程下载图片
package Thread; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; /** * 练习Thread,多线程下载图片 * * @author ZhaoLu cang on 2021/4/10 0010 */ public class Thread02 extends Thread{ private String name;//文件名 private String url;//地址 public Thread02(String url,String name){ this.url=url; this.name=name; } //执行体 @Override public void run() { WebDownloader webDownloader=new WebDownloader(); webDownloader.downloader(url,name); System.out.println("下载的文件名为"+name); } public static void main(String[] args) { Thread02 t1=new Thread02("https://pic.cnblogs.com/avatar/1280312/20190419133542.png","我的头像.jpg"); Thread02 t2=new Thread02("https://5b0988e595225.cdn.sohucs.com/images/20190108/c060afad8f594428a584e5efd8b64f23.jpeg","apache图标.jpg"); Thread02 t3=new Thread02("https://avatar.csdnimg.cn/1/7/D/1_weixin_43847567_1584060639.jpg","CSDN头像.jpg"); t1.start(); t2.start(); t3.start(); } } class WebDownloader{ //下载方法 public void downloader(String url,String name){ try { //爬取从地址到文件 FileUtils.copyURLToFile(new URL(url),new File(name)); }catch (IOException e){ System.out.println("IO异常,downloader方法"+e); } } }
实现Runnable接口
package Thread; /** * 创建线程方式二:Runnable方法 * * @author ZhaoLu cang on 2021/4/10 0010 */ public class Thread03 implements Runnable { //重写run方法 @Override public void run() { //run方法线程体 for(int i=0;i<20;i++){ System.out.println("学习多线程中......"); } } public static void main(String[] args) throws InterruptedException { //创建Runnable接口的实现类对象 Thread01 thread01=new Thread01(); //创建线程对象,通过线程对象来开启我们的线程 Thread thread=new Thread(thread01); new Thread(thread01).start(); //main主线程 for(int i=0;i<20;i++){ System.out.println("正在学习代码......"); } } }
多线程下载图片
package Thread; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; /** * 练习Thread,多线程下载图片 * * @author ZhaoLu cang on 2021/4/10 0010 */ public class Thread04 implements Runnable{ private String name;//文件名 private String url;//地址 public Thread04(String url,String name){ this.url=url; this.name=name; } //执行体 @Override public void run() { WebDownloader02 webDownloader=new WebDownloader02(); webDownloader.downloader(url,name); System.out.println("下载的文件名为"+name); } public static void main(String[] args) { Thread04 t1=new Thread04("https://pic.cnblogs.com/avatar/1280312/20190419133542.png","我的头像.jpg"); Thread04 t2=new Thread04("https://5b0988e595225.cdn.sohucs.com/images/20190108/c060afad8f594428a584e5efd8b64f23.jpeg","apache图标.jpg"); Thread04 t3=new Thread04("https://avatar.csdnimg.cn/1/7/D/1_weixin_43847567_1584060639.jpg","CSDN头像.jpg"); new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); } } class WebDownloader02{ //下载方法 public void downloader(String url,String name){ try { //爬取从地址到文件 FileUtils.copyURLToFile(new URL(url),new File(name)); }catch (IOException e){ System.out.println("IO异常,downloader方法"+e); } } }
实现Callable接口
多线程下载图片
package Thread; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.*; /** * 线程创建方式三:Callable * * @author ZhaoLu cang on 2021/4/10 0010 */ public class ThreadCallable implements Callable<Boolean>{ private String name;//文件名 private String url;//地址 public ThreadCallable(String url,String name){ this.url=url; this.name=name; } //执行体 @Override public Boolean call() { WebDownloader01 webDownloader=new WebDownloader01(); webDownloader.downloader(url,name); System.out.println("下载的文件名为"+name); return true; } public static void main(String[] args) throws ExecutionException, InterruptedException { ThreadCallable t1=new ThreadCallable("https://pic.cnblogs.com/avatar/1280312/20190419133542.png","我的头像.jpg"); ThreadCallable t2=new ThreadCallable("https://5b0988e595225.cdn.sohucs.com/images/20190108/c060afad8f594428a584e5efd8b64f23.jpeg","apache图标.jpg"); ThreadCallable t3=new ThreadCallable("https://avatar.csdnimg.cn/1/7/D/1_weixin_43847567_1584060639.jpg","CSDN头像.jpg"); //创建执行服务 ExecutorService ser= Executors.newFixedThreadPool(3); //提交执行 Future<Boolean> r1=ser.submit(t1); Future<Boolean> r2=ser.submit(t2); Future<Boolean> r3=ser.submit(t3); //获取结果 boolean rs1=r1.get(); boolean rs2=r2.get(); boolean rs3=r3.get(); //关闭服务 ser.shutdown(); } } class WebDownloader01{ //下载方法 public void downloader(String url,String name){ try { //爬取从地址到文件 FileUtils.copyURLToFile(new URL(url),new File(name)); }catch (IOException e){ System.out.println("IO异常,downloader方法"+e); } } }