自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

線程池監(jiān)控:執(zhí)行超時(shí)、等待超時(shí);執(zhí)行超時(shí)數(shù)量、等待超時(shí)數(shù)量

開發(fā) 前端
線程池 ThreadPoolExecutor 為了提供擴(kuò)展,提供了兩個(gè)方法 beforeExecute 和 afterExecute,每個(gè)任務(wù)執(zhí)行前后都會調(diào)用這兩個(gè)方法,相當(dāng)于對線程任務(wù)的執(zhí)行做了一個(gè)切面。

?監(jiān)控線程池:執(zhí)行超時(shí)、等待超時(shí);執(zhí)行超時(shí)數(shù)量、等待超時(shí)數(shù)量;

擴(kuò)展線程池 ThreadPoolExecutor 的兩個(gè)方法 beforeExecute 和 afterExecute

自定義Runnable 記錄關(guān)鍵節(jié)點(diǎn)時(shí)間

關(guān)鍵時(shí)間節(jié)點(diǎn)參數(shù):

  • 任務(wù)創(chuàng)建(提交)時(shí)間:submitTime
  • 任務(wù)開始執(zhí)行時(shí)間:startExeTime
  • 任務(wù)結(jié)束執(zhí)行時(shí)間:endExeTime
  • 任務(wù)在隊(duì)列等待時(shí)間:任務(wù)開始執(zhí)行時(shí)間 - 任務(wù)創(chuàng)建(提交)時(shí)間
  • 任務(wù)執(zhí)行總時(shí)間:任務(wù)結(jié)束執(zhí)行時(shí)間 - 任務(wù)開始執(zhí)行時(shí)間

源碼分析

線程池 ThreadPoolExecutor 為了提供擴(kuò)展,提供了兩個(gè)方法 beforeExecute 和 afterExecute,每個(gè)任務(wù)執(zhí)行前后都會調(diào)用這兩個(gè)方法,相當(dāng)于對線程任務(wù)的執(zhí)行做了一個(gè)切面。

public class ThreadPoolExecutor extends AbstractExecutorService {
/**
* @param t 執(zhí)行任務(wù)的線程
* @param
protected void beforeExecute(Thread t, Runnable r){ }

/**
* @param r 將要被執(zhí)行的任務(wù)
* @param
protected void afterExecute(Runnable r, Throwable t){ }
}

源碼執(zhí)行邏輯:

線程池?cái)U(kuò)展代碼:

public class ThreadPoolExpandTest {
// 定義線程池
public static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
2,
4,
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),
new ThreadPoolExecutor.DiscardOldestPolicy()
){
@Override
/**
* @param t 執(zhí)行任務(wù)的線程
* @param
protected void beforeExecute(Thread t, Runnable r){
System.out.println("beforeExecute將要被執(zhí)行");
}

/**
* @param r 將要被執(zhí)行的任務(wù)
* @param
@Override
protected void afterExecute(Runnable r, Throwable t){
System.out.println("afterExecute已經(jīng)執(zhí)行完畢");
}
};
public static void main(String[] args){
poolExecutor.execute(()->{
System.out.println("任務(wù)執(zhí)行");
});
}
}

運(yùn)行結(jié)果:

beforeExecute執(zhí)行
任務(wù)執(zhí)行
afterExecute執(zhí)行

總結(jié):從測試代碼可以看出,通過擴(kuò)展線程池參數(shù)可以進(jìn)行任務(wù)執(zhí)行的監(jiān)控。

自定義Runnable

通過自定義Runnable,記錄任務(wù)執(zhí)行的一些時(shí)間:

  • 任務(wù)創(chuàng)建(提交)時(shí)間
  • 任務(wù)開始執(zhí)行時(shí)間
public class DynamicRunnable implements Runnable{
/**
* runnable
*/
private final Runnable runnable;
/**
* 任務(wù)創(chuàng)建(提交)時(shí)間
*/
private final Long submitTime;
/**
* 任務(wù)開始執(zhí)行時(shí)間
*/
private Long startExeTime;

public DynamicRunnable(Runnable runnable){
this.runnable = runnable;
submitTime = System.currentTimeMillis();
}

@Override
public void run(){
runnable.run();
}

public Long getSubmitTime(){
return submitTime;
}

public void setStartExeTime(Long startExeTime){
this.startExeTime = startExeTime;
}

public Long getStartExeTime(){
return startExeTime;
}
}

繼承線程池+自定義Runnable

核心參數(shù):

/**
* 執(zhí)行超時(shí),單位(毫秒)
*/
private long runTimeout;

/**
* 等待超時(shí),單位(毫秒)
*/
private long queueTimeout;

/**
* 執(zhí)行超時(shí)數(shù)量
*/
private final AtomicInteger runTimeoutCount = new AtomicInteger();

/**
* 等待超時(shí)數(shù)量
*/
private final AtomicInteger queueTimeoutCount = new AtomicInteger();

重寫ThreadPoolExecutor方法:

@Override
public void execute(Runnable command){
if (runTimeout > 0 || queueTimeout > 0) {
// 記錄任務(wù)提交時(shí)間
command = new DynamicRunnable(command);
}
super.execute(command);
}
@Override
protected void beforeExecute(Thread t, Runnable r){
if (!(r instanceof DynamicRunnable)) {
super.beforeExecute(t, r);
return;
}
DynamicRunnable runnable = (DynamicRunnable) r;
long currTime = System.currentTimeMillis();
if (runTimeout > 0) {
// 記錄任務(wù)開始執(zhí)行時(shí)間
runnable.setStartExeTime(currTime);
}
if (queueTimeout > 0) {
// 任務(wù)開始執(zhí)行時(shí)間 - 任務(wù)創(chuàng)建(提交)時(shí)間
long waitTime = currTime - runnable.getSubmitTime();
if (waitTime > queueTimeout) {
log.error("{} execute queue timeout waitTime: {}ms", this.getThreadPoolName(),waitTime);
}
}
super.beforeExecute(t, r);
}
@Override
protected void afterExecute(Runnable r, Throwable t){
if (runTimeout > 0) {
DynamicRunnable runnable = (DynamicRunnable) r;
// 任務(wù)執(zhí)行總時(shí)間:任務(wù)結(jié)束執(zhí)行時(shí)間 - 任務(wù)開始執(zhí)行時(shí)間
long runTime = System.currentTimeMillis() - runnable.getStartExeTime();
if (runTime > runTimeout) {
runTimeoutCount.incrementAndGet();
log.error("{} execute, run timeout runTime: {}ms", this.getThreadPoolName(), runTime);
}
}
super.afterExecute(r, t);
}

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2020-04-23 11:43:55

MySQL數(shù)據(jù)庫SQL

2017-08-30 17:21:05

LinuxShell超時(shí)現(xiàn)象

2009-06-11 16:44:06

超時(shí)控制Java線程

2012-02-15 13:26:39

JavaJava Socket

2021-02-22 17:18:35

MySQLSQL行鎖

2013-04-03 11:07:46

JavaJava線程

2024-04-30 12:56:00

多線程.NET

2017-06-04 16:24:27

線程線程池中斷

2009-07-21 14:32:51

ASP.NET進(jìn)程模型

2012-08-09 09:11:32

PHP超時(shí)

2014-03-18 13:27:55

Redis數(shù)據(jù)存儲

2021-07-01 09:42:08

Redisson分布式

2024-02-26 08:50:37

訂單自動取消消息

2021-11-15 12:42:25

C# 定位gRPC

2024-12-26 12:59:39

2010-11-25 11:15:11

MySQL查詢超時(shí)

2010-10-14 09:15:20

MySQL查詢

2010-11-08 15:21:17

SQL Server連

2022-10-14 08:18:07

Guavaweb應(yīng)用

2022-08-28 19:58:59

LinkerdKubernetes
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號