溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java guava monitor監(jiān)視器線程怎么用

發(fā)布時間:2021-11-02 15:42:43 來源:億速云 閱讀:125 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Java guava monitor監(jiān)視器線程怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

Maven依賴

<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>

代碼

不廢話上代碼。

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)造對象的時候需要傳遞泛型的校驗(yàn)對象,以及斷言和需要執(zhí)行的Runable。

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

Java guava monitor監(jiān)視器線程怎么用

以上是“Java guava monitor監(jiān)視器線程怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI