少学习多摸鱼

day21-线程

线程

创建线程方式:

  1. 继承thread类,重写run()方法,调用start开启线程

 
 1 public class TestThread extends Thread{
 2      @Override
 3      public void run() {
 4          //run方法线程体
 5          for (int i = 0; i < 200; i++) {
 6              System.out.println("seeing code---"+i);
 7  8          }
 9      }
10 11      public static void main(String[] args) {
12          //main线程,主线程
13          //创建一共线程对象
14          TestThread testThread = new TestThread();
15          //调用start方法开启线程 start方法多条线程并行执行
16          //调用run方法开启线程,只有一条执行路径
17          testThread.start();
18 19          for (int i = 0; i < 1000; i++) {
20              System.out.println("learn thread---"+i);
21 22          }
23      }
24  }

 

运行时main程序和线程交替运行。

线程开启不一定立即执行,由cpu调度执行

  1. 创建runnable接口,重写run方法,执行线程需要丢入runnable接口的实现类,调用start方法

 
 1 public class TestThread3 implements Runnable{
 2      @Override
 3      public void run() {
 4          //run方法线程体
 5          for (int i = 0; i < 200; i++) {
 6              System.out.println("seeing code---"+i);
 7  8          }
 9      }
10 11      public static void main(String[] args) {
12 13          //创建runnable接口的实现类对象
14          TestThread3 testThread3 = new TestThread3();
15          //创建线程对象,通过线程对象来开启线程(代理)
16  //        Thread thread = new Thread(testThread3);
17  //        thread.start();
18          //可以简写
19          new Thread(testThread3).start();
20 21          for (int i = 0; i < 1000; i++) {
22              System.out.println("learn thread---"+i);
23 24          }
25      }
26  }

 

总结

 1  1. 继承Thread类
 2     具备多线程能力
 3     启动:子类对象.start();
 4     不建议使用:避免单继承局限性
 5  6  2. 实现runnable接口
 7     具有多线程能力
 8     传入目标对象+Thread对象.start();
 9     new Thread(目标对象).start();
10     建议使用:避免单继承局限性,方便同一个对象被多个线程使用

 

练习 实现多线程同步下载照片

  1. 首先写一个下载器方法

    传入照片网址,名称

 
 1 class WebDownloader{
 2      //下载方法
 3      public void downloader(String url,String name){
 4          try {
 5              FileUtils.copyURLToFile(new URL(url),new File(name));
 6          } catch (IOException e) {
 7              e.printStackTrace();
 8              System.out.println("I/O异常,downloader出现异常");
 9          }
10      }
11  }

 

  1. 在类中创建对象,赋值重写run()方法创建下载器对象,对图片进行下载

  2. 在主函数中创建类的对象传入地址和名字,用start方法调用开启线程

 
 1 public class TestThread2 implements Runnable{
 2      private String url;//网络图片地址
 3      private String name;//保存文件名字
 4  5      public TestThread2(String url,String name){
 6          this.url = url;
 7          this.name = name;
 8      }
 9      @Override
10      public void run() {
11          WebDownloader webDownloader = new WebDownloader();
12          webDownloader.downloader(url,name);
13          System.out.println("下载的文件名为:" +name);
14 15      }
16 17      public static void main(String[] args) {
18          TestThread2 t1 = new TestThread2("https://bkimg.cdn.bcebos.com/pic/503d269759ee3d6d55fb38bad2467a224f4a20a4703b?x-bce-process=image/resize,m_lfit,h_500,limit_1","1.jpg");
19          TestThread2 t2 = new TestThread2("https://bkimg.cdn.bcebos.com/pic/908fa0ec08fa513d2697deb4ac3d42fbb2fb43167302?x-bce-process=image/resize,m_lfit,h_500,limit_1","2.jpg");
20          TestThread2 t3 = new TestThread2("https://bkimg.cdn.bcebos.com/pic/b999a9014c086e061d957f3a93586cf40ad162d95d3b?x-bce-process=image/resize,m_lfit,h_500,limit_1","3.jpg");
21 22          new Thread(t1).start();
23          new Thread(t2).start();
24          new Thread(t3).start();
25 26      }
27  }

 

 
posted @ 2022-10-24 16:34  北海之上  阅读(27)  评论(0)    收藏  举报
/* 粒子吸附*/