一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java guava monitor監(jiān)視器線程的使用詳解

Java guava monitor監(jiān)視器線程的使用詳解

2022-03-09 00:47劍客阿良_ALiang Java教程

工作中的場景中是否存在類似這樣的場景,需要提交的線程在某個(gè)觸發(fā)條件下執(zhí)行。本文主要就是使用guava中的monitor來優(yōu)雅的實(shí)現(xiàn)帶監(jiān)視器的線程

Maven依賴

?
1
2
3
4
5
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

代碼

不廢話上代碼。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.huyi.csdn.tools;
 
import cn.hutool.core.thread.ThreadUtil;
import com.google.common.util.concurrent.Monitor;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
 
/**
 * @Program: csdn @ClassName: MonitorRunner @Author: huyi @Date: 2021-10-30 15:22 @Description:
 * 監(jiān)視器Runner @Version: V1.0
 */
public class MonitorRunner<T> implements Runnable {
  private T param;
  private Function<T, Boolean> condition;
  private Runnable runnable;
  private Monitor monitor;
 
  /**
   * 構(gòu)造函數(shù)
   *
   * @param param 判斷參數(shù)
   * @param condition 判定函數(shù)
   * @param runnable 執(zhí)行內(nèi)容
   */
  public MonitorRunner(T param, Function<T, Boolean> condition, Runnable runnable) {
    this.param = param;
    this.condition = condition;
    this.runnable = runnable;
    monitor = new Monitor();
  }
 
  @Override
  public void run() {
    System.out.println("線程開始");
    Monitor.Guard guard =
        new Monitor.Guard(monitor) {
          @Override
          public boolean isSatisfied() {
            return condition.apply(param);
          }
        };
 
    while (true) {
      if (monitor.enterIf(guard)) {
        try {
          runnable.run();
        } finally {
          monitor.leave();
          break;
        }
      } else {
        continue;
      }
    }
  }
 
  public T getParam() {
    return param;
  }
 
  public MonitorRunner<T> setParam(T param) {
    this.param = param;
    return this;
  }
 
  public static void main(String[] args) {
    ExecutorService executorService =
        Executors.newFixedThreadPool(10, new CustomizableThreadFactory("MONITOR-"));
    MonitorRunner<Integer> monitorRunner =
        new MonitorRunner<>(
            0,
            x -> x > 10,
            () -> {
              // todo 線程需要執(zhí)行的內(nèi)容
              System.out.println("今天天氣真好");
            });
    executorService.submit(monitorRunner);
    while (monitorRunner.getParam() <= 10) {
      monitorRunner.setParam(monitorRunner.getParam() + 1);
      ThreadUtil.sleep(1000L);
      System.out.println("當(dāng)前Param的值:" + monitorRunner.getParam());
    }
    ThreadUtil.sleep(5000L);
    executorService.shutdown();
  }
}

代碼說明

主要在構(gòu)造對象的時(shí)候需要傳遞泛型的校驗(yàn)對象,以及斷言和需要執(zhí)行的Runable。

執(zhí)行結(jié)果

Java guava monitor監(jiān)視器線程的使用詳解

總結(jié)

沒啥好總結(jié)的,看場景使用吧。

如果本文對你有用,請點(diǎn)個(gè)贊吧,謝謝。

Java guava monitor監(jiān)視器線程的使用詳解

到此這篇關(guān)于Java guava monitor監(jiān)視器線程的使用詳解的文章就介紹到這了,更多相關(guān)Java 監(jiān)視器線程內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://huyi-aliang.blog.csdn.net/article/details/121056404

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩中文字幕在线不卡 | 95视频在线观看在线分类h片 | 日本动漫打扑克动画片樱花动漫 | 男女被爆动漫羞羞动漫 | 网友偷自拍原创区 | 热99re久久精品国产 | 国产草逼视频 | 国产欧美日韩专区毛茸茸 | 青草国产福利视频免费观看 | 精品一区二区三区视频日产 | 成年人视频在线 | 天堂va在线高清一区 | 男模chinesegayxxxx | 国产一区二区在线观看美女 | 好大好想要 | 91亚洲一区二区在线观看不卡 | 色cccwww在线播放| 精品午夜视频 | 国产亚洲精品看片在线观看 | 欧美日韩国产另类一区二区三区 | 91麻豆精品| 久久视频在线视频观看天天看视频 | 动漫jk美女被爆羞羞漫画 | a免费看| 日本h片在线 | jj视频免费| gay帅老头毛都白了 gayxxx视频 | 日韩精品亚洲专区在线影视 | 免费午夜剧场 | 性的张力 | 亚瑟天堂久久一区二区影院 | 亚洲国产日韩欧美mv | 美女被躁爽死 | ak福利午夜在线观看 | 国产裸舞福利资源在线视频 | 欧美日韩综合一区 | 日本天堂视频在线观看 | 午夜精品久久久内射近拍高清 | 国产久热香蕉在线观看 | 国产精品99爱免费视频 | 暖暖日本高清 |