Jetty的threadpool模組

jackieathome發表於2024-03-06

Jetty提供的執行緒池相關的模組,如下:

  • threadpool
  • threadpool-virtual,使用JDK 21提供的virtual threads。
  • threadpool-virtual-preview,使用JDK 19和JDK 20。

注意上述模組不能共存。
啟用threadpool模組後再啟用threadpool-virtual模組時,將會有類似如下的提示:

ERROR : Module threadpool-virtual provides threadpool, which is already provided by threadpool enabled in [${jetty.base}/start.d/threadpool.ini]

Usage: java -jar $JETTY_HOME/start.jar [options] [properties] [configs]
       java -jar $JETTY_HOME/start.jar --help  # for more information

threadpool

啟用threadpool模組,執行如下命令:

java -jar $JETTY_HOME/start.jar --add-modules=threadpool

命令的輸出,如下:

INFO  : threadpool      initialized in ${jetty.base}/start.d/threadpool.ini
INFO  : Base directory was modified

threadpool模組的配置檔案$JETTY_BASE/start.d/threadpool.ini,內容如下:

# ---------------------------------------
# Module: threadpool
# Enables and configures the Server ThreadPool.
# ---------------------------------------
--modules=threadpool

## Thread name prefix.
#jetty.threadPool.namePrefix=qtp<hashCode>

## Minimum number of pooled threads.
#jetty.threadPool.minThreads=10

## Maximum number of pooled threads.
#jetty.threadPool.maxThreads=200

## Number of reserved threads (-1 for heuristic).
#jetty.threadPool.reservedThreads=-1

## Whether to use virtual threads, if the runtime supports them.
## Deprecated, use Jetty module 'threadpool-virtual' instead.
#jetty.threadPool.useVirtualThreads=false

## Thread idle timeout (in milliseconds).
#jetty.threadPool.idleTimeout=60000

## The max number of idle threads that are evicted in one idleTimeout period.
#jetty.threadPool.maxEvictCount=1

## Whether to output a detailed dump.
#jetty.threadPool.detailedDump=false

各引數的說明,如下:

  • jetty.threadPool.namePrefix
    執行緒池中各執行緒的名稱字首。
  • jetty.threadPool.minThreads
    執行緒池中執行緒的最小數量。
  • jetty.threadPool.maxThreads
    執行緒池中執行緒的最大數量。
  • jetty.threadPool.reservedThreads
    執行緒池中保留的執行緒的數量。
  • jetty.threadPool.useVirtualThreads
    不推薦使用。對於JDK 21,推薦使用threadpool-virtual模組。
  • jetty.threadPool.idleTimeout
    空閒執行緒從執行緒池中移除前等待的時長,單位:毫秒,預設值:60000,即60秒。
  • jetty.threadPool.maxEvictCount
    清理空閒執行緒時,單次操作中被移除掉的執行緒的數量。
  • jetty.threadPool.detailedDump
    是否匯出執行緒池中各執行緒的完整的棧。預設值為false,即只輸出棧頂。

threadpool-virtual

啟用threadpool-virtual模組,執行如下命令:

java -jar $JETTY_HOME/start.jar --add-modules=threadpool-virtual

啟用threadpool-virtual模組成功時的輸出,如下:

INFO  : threadpool-virtual initialized in ${jetty.base}/start.d/threadpool-virtual.ini
INFO  : Base directory was modified

threadpool-virtual模組的配置檔案$JETTY_BASE/start.d/threadpool-virtual.ini,內容如下:

# ---------------------------------------
# Module: threadpool-virtual
# Enables and configures the Server ThreadPool with support for virtual threads in Java 21 or later.
# ---------------------------------------
--modules=threadpool-virtual

## Platform threads name prefix.
#jetty.threadPool.namePrefix=qtp<hashCode>

## Minimum number of pooled threads.
#jetty.threadPool.minThreads=10

## Maximum number of pooled threads.
#jetty.threadPool.maxThreads=200

## Number of reserved threads (-1 for heuristic).
#jetty.threadPool.reservedThreads=-1

## Thread idle timeout (in milliseconds).
#jetty.threadPool.idleTimeout=60000

## The max number of idle threads that can be evicted in one idleTimeout period.
#jetty.threadPool.maxEvictCount=1

## Whether to output a detailed dump.
#jetty.threadPool.detailedDump=false

## Virtual threads name prefix.
#jetty.threadPool.virtual.namePrefix=qtp<hashCode>-virtual-

## Whether virtual threads inherits the values of inheritable thread locals.
#jetty.threadPool.virtual.inheritInheritableThreadLocals=true

各引數的說明,如下:

  • jetty.threadPool.namePrefix
    threadPool
  • jetty.threadPool.minThreads
    threadPool
  • jetty.threadPool.maxThreads
    threadPool
  • jetty.threadPool.reservedThreads
    threadPool
  • jetty.threadPool.idleTimeout
    threadPool
  • jetty.threadPool.maxEvictCount
    threadPool
  • jetty.threadPool.detailedDump
    threadPool
  • jetty.threadPool.virtual.namePrefix
    virtual threads的執行緒名稱的字首。
  • jetty.threadPool.virtual.inheritInheritableThreadLocals
    是否複用ThreadLocal物件。預設值為true

threadpool-virtual-preview

啟用threadpool-virtual-preview模組,執行如下命令:

java -jar $JETTY_HOME/start.jar --add-modules=threadpool-virtual-preview

啟用threadpool-virtual-preview模組成功時的輸出,如下:

INFO  : threadpool-virtual-preview initialized in ${jetty.base}/start.d/threadpool-virtual-preview.ini
INFO  : Base directory was modified

threadpool-virtual-preview模組的配置檔案$JETTY_BASE/start.d/threadpool-virtual-preview.ini,內容如下:

# ---------------------------------------
# Module: threadpool-virtual-preview
# Enables and configures the Server ThreadPool with support for virtual threads in Java 19 and Java 20.
# ---------------------------------------
--modules=threadpool-virtual-preview

## Platform threads name prefix.
#jetty.threadPool.namePrefix=qtp<hashCode>

## Minimum number of pooled threads.
#jetty.threadPool.minThreads=10

## Maximum number of pooled threads.
#jetty.threadPool.maxThreads=200

## Number of reserved threads (-1 for heuristic).
#jetty.threadPool.reservedThreads=-1

## Thread idle timeout (in milliseconds).
#jetty.threadPool.idleTimeout=60000

## The max number of idle threads that can be evicted in one idleTimeout period.
#jetty.threadPool.maxEvictCount=1

## Whether to output a detailed dump.
#jetty.threadPool.detailedDump=false

## Virtual threads name prefix.
#jetty.threadPool.virtual.namePrefix=qtp<hashCode>-virtual-

## Whether virtual threads are allowed to set thread locals.
#jetty.threadPool.virtual.allowSetThreadLocals=true

## Whether virtual threads inherits the values of inheritable thread locals.
#jetty.threadPool.virtual.inheritInheritableThreadLocals=true

各引數的說明,如下:

  • jetty.threadPool.namePrefix
    threadPool
  • jetty.threadPool.minThreads
    threadPool
  • jetty.threadPool.maxThreads
    threadPool
  • jetty.threadPool.reservedThreads
    threadPool
  • jetty.threadPool.idleTimeout
    threadPool
  • jetty.threadPool.maxEvictCount
    threadPool
  • jetty.threadPool.detailedDump
    threadPool
  • jetty.threadPool.virtual.namePrefix
    virtual threads的執行緒名稱的字首。
  • jetty.threadPool.virtual.allowSetThreadLocals
    是否允許virtual threads記錄ThreadLocal型別的物件。預設值為true
  • jetty.threadPool.virtual.inheritInheritableThreadLocals
    是否複用ThreadLocal物件。預設值為true

參考資料

  • 虛擬執行緒原理及效能分析
  • 聊一聊JDK21-虛擬執行緒
  • Java高併發革命!JDK19新特性——虛擬執行緒(Virtual Threads)
  • Virtual Threads 虛擬執行緒

相關文章