首页 > 其他分享 >day19 -线程2

day19 -线程2

时间:2022-10-26 22:23:33浏览次数:45  
标签:String TestCallable void public 线程 new day19 name

线程

  1. 使用callable创建线程

好处:

  1. 可以定义返回值

  2. 可以抛出异常

同样定义下载器

 
 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  }

 

使用callable方法需要四个步骤

  1. 创建执行服务

  2. 提交执行任务

  3. 获取执行结果

  4. 关闭服务

 1  public class TestCallable implements Callable<Boolean> {
 2      private String url;//网络图片地址
 3      private String name;//保存文件名字
 4  ​
 5      public TestCallable(String url,String name){
 6          this.url = url;
 7          this.name = name;
 8      }
 9      @Override
10      public Boolean call() {
11          WebDownloader webDownloader = new WebDownloader();
12          webDownloader.downloader(url,name);
13          System.out.println("下载的文件名为:" +name);
14          return true;
15  ​
16      }
17  ​
18      public static void main(String[] args) throws ExecutionException, InterruptedException {
19          TestCallable t1 = new TestCallable("https://bkimg.cdn.bcebos.com/pic/503d269759ee3d6d55fb38bad2467a224f4a20a4703b?x-bce-process=image/resize,m_lfit,h_500,limit_1","1.jpg");
20          TestCallable t2 = new TestCallable("https://bkimg.cdn.bcebos.com/pic/908fa0ec08fa513d2697deb4ac3d42fbb2fb43167302?x-bce-process=image/resize,m_lfit,h_500,limit_1","2.jpg");
21          TestCallable t3 = new TestCallable("https://bkimg.cdn.bcebos.com/pic/b999a9014c086e061d957f3a93586cf40ad162d95d3b?x-bce-process=image/resize,m_lfit,h_500,limit_1","3.jpg");
22          //创建执行服务
23          ExecutorService ser = Executors.newFixedThreadPool(3);
24          //提交执行
25          Future<Boolean> result1 = ser.submit(t1);
26          Future<Boolean> result2 = ser.submit(t2);
27          Future<Boolean> result3 = ser.submit(t3);
28          //获取结果
29          boolean r1 = result1.get();
30          boolean r2 = result2.get();
31          boolean r3 = result3.get();
32          //关闭服务
33          ser.shutdown();
34  ​
35  ​
36      }
37  }

 

静态模式代理

使用代理将不同的任务分给其他对象去做

创建接口:

 
1 interface Marry{
2      void HappyMarry();
3  }

 

真实对象:

1  class You implements Marry{
2      @Override
3      public void HappyMarry() {
4          System.out.println("开心结婚奥");
5      }
6  }

 

代理角色:

 1  class WeddingCompany implements Marry{
 2      private Marry target;
 3  ​
 4      public WeddingCompany(Marry target) {
 5          this.target = target;
 6      }
 7  ​
 8      @Override
 9      public void HappyMarry() {
10          before();
11          this.target.HappyMarry();
12          after();
13  ​
14      }
15  ​
16      private void after() {
17          System.out.println("结婚之后,收尾款");
18      }
19  ​
20      private void before() {
21          System.out.println("结婚之前,布置现场");
22      }
23  }

 

实现:

1  public class StaticProxy {
2      public static void main(String[] args) {
3          WeddingCompany weddingCompany = new WeddingCompany(new You());
4          weddingCompany.HappyMarry();
5      }
6  ​
7  }

 

 

标签:String,TestCallable,void,public,线程,new,day19,name
From: https://www.cnblogs.com/GUGUZIZI/p/16830311.html

相关文章