解決方案之動態可伸縮執行緒池

2021-09-26 07:22:55 字數 2431 閱讀 2196

在程式開發過程中為了充分合理的協調利用系統資源【cpu、memory、i/o等】,避免頻繁地建立與銷毀執行緒資源問題,常用解決方案是建立執行緒池,如下是個人設計的動態可伸縮執行緒池方案。

/**

* 動態可伸縮執行緒池

* * @author tansheng

* @version $$id$$

* @since 1.0

*/@slf4j

public

class

threadhelper

maxinumpoolsize = corepoolsize *3;

if(maxinumpoolsize>maxinum_pool_size)

queuecapacity = corepoolsize *2;

queue =

newarrayblockingqueue

(queuecapacity);}

/** * getthreadexecutor

** @return

*/private

static executorservice getthreadexecutor()

};rejectedexecutionhandler rejectedhandler =

newrejectedexecutionhandler()

thread pool's blocking queue depth is: {}, active threads ={}, "

+" pool size = {},task count = {}"

, threadname, depthqueue, executor.

getactivecount()

, executor.

getpoolsize()

, executor.

gettaskcount()

);thread.

sleep

(waitmillis);}

catch

(interruptedexception ex)

} executor.

submit

(r);}}

; executorservice =

newthreadpoolexecutor

(corepoolsize, maxinumpoolsize, keepaliveseconds,

timeunit.seconds, queue, threadfactory, rejectedhandler)

; runtime.

getruntime()

.addshutdownhook

(new

thread()

}});

}}}return executorservice;

}/**

* execute* *

* @param task

*/public

static

future

execute

(callable

task)

return

getthreadexecutor()

.submit

(task);}

/** * execute* *

* @param task

*/public

static

void

execute

(runnable task)

getthreadexecutor()

.submit

(task);}

/** * execute* *

* @param task

* @param result

*/public

static

future

execute

(runnable task, t result)

return

getthreadexecutor()

.submit

(task, result);}

/** * 批量執行* *

* @param tasks

*/public

static

list

>

batchexecute

(collection<

?extends

callable

> tasks)

throws interruptedexception

else

return futures;

}}

可定製智慧型系統解決方案

架構如下 抽象出模組 一 內部執行 1 規則模組 condition 條件 接輸入介面,通過乙個 控制器判斷 action 動作。接硬體通用介面 rule 規則。rule由2部分組成,condition和action action可以迭代condition.形成乙個複雜的邏輯樹,action con...

積累之解決方案

這是剛學習vb.net 的時候寫的部落格,覺得也是一種積累,就貼出來了。在使用vs visual studio 的過程中,大家一定遇到過敲完 之後,執行出錯,除錯多次也沒有解決。最終生成一下解決方案就ok 了。這裡來了解一下熟悉而陌生的解決方案。乙個解決方案可以有多個專案,如果當前解決方案只有乙個專...

執行緒 linux之thread錯誤解決方案

1.錯誤現象 undefined reference to pthread create undefined reference to pthread join 2.問題原因 pthread 庫不是 linux 系統預設的庫,連線時需要使用靜態庫 libpthread.a,所以在使用 pthread...