java 如何根據(jù)內(nèi)存占用情況調(diào)整開線程的數(shù)量?
問題描述
問題解答
回答1:setMaximumPoolSize 是否動態(tài)有效看下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<Worker> 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 <tt>RejectedExecutionHandler</tt>. * * @param command the task to execute * @throws RejectedExecutionException at discretion of * <tt>RejectedExecutionHandler</tt>, 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} }
相關(guān)文章:
1. boot2docker無法啟動2. java - SSH框架中寫分頁時service層中不能注入分頁類3. docker-compose中volumes的問題4. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””5. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.6. javascript - mock.js可以存儲數(shù)據(jù)嗎7. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?8. javascript - vue生成一維碼?求助!!!!!急9. nignx - docker內(nèi)nginx 80端口被占用10. java - Spring事務(wù)回滾問題

網(wǎng)公網(wǎng)安備