java 监控线程内存占用情况_java 如何根据内存占用情况调整开线程的数量?

setMaximumPoolSize 是否动态有效看下jdk源码不就知道了

/**

* Sets the maximum allowed number of threads. This overrides any

* value set in the constructor. If the new value is smaller than

* the current value, excess existing threads will be

* terminated when they next become idle.

*

* @param maximumPoolSize the new maximum

* @throws IllegalArgumentException if the new maximum is

* less than or equal to zero, or

* less than the {@linkplain #getCorePoolSize core pool size}

* @see #getMaximumPoolSize

*/

public void setMaximumPoolSize(int maximumPoolSize) {

if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)

throw new IllegalArgumentException();

final ReentrantLock mainLock = this.mainLock;

mainLock.lock();

try {

int extra = this.maximumPoolSize - maximumPoolSize;

this.maximumPoolSize = maximumPoolSize;

if (extra > 0 && poolSize > maximumPoolSize) {

try {

Iterator it = workers.iterator();

while (it.hasNext() &&

extra > 0 &&

poolSize > maximumPoolSize) {

it.next().interruptIfIdle();

--extra;

}

} catch (SecurityException ignore) {

// Not an error; it is OK if the threads stay live

}

}

} finally {

mainLock.unlock();

}

}

execute方法:

/**

* Executes the given task sometime in the future. The task

* may execute in a new thread or in an existing pooled thread.

*

* If the task cannot be submitted for execution, either because this

* executor has been shutdown or because its capacity has been reached,

* the task is handled by the current RejectedExecutionHandler.

*

* @param command the task to execute

* @throws RejectedExecutionException at discretion of

* RejectedExecutionHandler, if task cannot be accepted

* for execution

* @throws NullPointerException if command is null

*/

public void execute(Runnable command) {

if (command == null)

throw new NullPointerException();

if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {

if (runState == RUNNING && workQueue.offer(command)) {

if (runState != RUNNING || poolSize == 0)

ensureQueuedTaskHandled(command);

}

else if (!addIfUnderMaximumPoolSize(command))

reject(command); // is shutdown or saturated

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值