/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.tomcat.util.threads;
/** * As task queue specifically designed to run with a thread pool executor. The * task queue is optimised to properly utilize threads within a thread pool * executor. If you use a normal queue, the executor will spawn threads when * there are idle threads and you won't be able to force items onto the queue * itself.
*/ publicclass TaskQueue extends LinkedBlockingQueue<Runnable> {
privatestaticfinallong serialVersionUID = 1L; protectedstaticfinal StringManager sm = StringManager.getManager(TaskQueue.class);
/** * Used to add a task to the queue if the task has been rejected by the Executor. * * @param o The task to add to the queue * * @return {@code true} if the task was added to the queue, * otherwise {@code false}
*/ publicboolean force(Runnable o) { if (parent == null || parent.isShutdown()) { thrownew RejectedExecutionException(sm.getString("taskQueue.notRunning"));
} returnsuper.offer(o); //forces the item onto the queue, to be used if the task is rejected
}
@Override publicboolean offer(Runnable o) { //we can't do any checks if (parent==null) { returnsuper.offer(o);
} //we are maxed out on threads, simply queue the object if (parent.getPoolSizeNoLock() == parent.getMaximumPoolSize()) { returnsuper.offer(o);
} //we have idle threads, just add it to the queue if (parent.getSubmittedCount() <= parent.getPoolSizeNoLock()) { returnsuper.offer(o);
} //if we have less threads than maximum force creation of a new thread if (parent.getPoolSizeNoLock() < parent.getMaximumPoolSize()) { returnfalse;
} //if we reached here, we need to add it to the queue returnsuper.offer(o);
}
@Override public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
Runnable runnable = super.poll(timeout, unit); if (runnable == null && parent != null) { // the poll timed out, it gives an opportunity to stop the current // thread if needed to avoid memory leaks.
parent.stopCurrentThreadIfNeeded();
} return runnable;
}
@Override public Runnable take() throws InterruptedException { if (parent != null && parent.currentThreadShouldBeStopped()) { return poll(parent.getKeepAliveTime(TimeUnit.MILLISECONDS),
TimeUnit.MILLISECONDS); // yes, this may return null (in case of timeout) which normally // does not occur with take() // but the ThreadPoolExecutor implementation allows this
} returnsuper.take();
}
}
¤ Dauer der Verarbeitung: 0.23 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.