Thank you, I know the role of TaskQueue, but the comment about "normal queue" 
on the TaskQueue class is still incomprehensible.


In the java.util.concurrent.ThreadPoolExecutor#execute method, the comment 
mentions: "3. If we cannot queue task, then we try to add a new thread. If it 
fails, we know we are shut down or saturated and so reject the task.". Explain 
that the JDK prioritizes putting tasks into the queue rather than creating 
threads first, which is not consistent with the statement that "normal queue" 
mentioned in TaskQueue prioritizes thread creation.


java.util.concurrent.ThreadPoolExecutor#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 {@code RejectedExecutionHandler}.
 *
 * @param command the task to execute
 * @throws RejectedExecutionException at discretion of
 *         {@code RejectedExecutionHandler}, if 
the task
 *         cannot be accepted for execution
 * @throws NullPointerException if {@code command} is null
 */
public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    /*
     * Proceed in 3 steps:
     *
     * 1. If fewer than corePoolSize threads are running, try to
     * start a new thread with the given command as its first
     * task.  The call to addWorker atomically checks 
runState and
     * workerCount, and so prevents false alarms that would add
     * threads when it shouldn't, by returning false.
     *
     * 2. If a task can be successfully queued, then we still 
need
     * to double-check whether we should have added a thread
     * (because existing ones died since last checking) or that
     * the pool shut down since entry into this method. So we
     * recheck state and if necessary roll back the enqueuing if
     * stopped, or start a new thread if there are none.
     *
     * 3. If we cannot queue task, then we try to add a new
     * thread.  If it fails, we know we are shut down or 
saturated
     * and so reject the task.
     */
    int c = ctl.get();
&nbsp; &nbsp; if (workerCountOf(c) < corePoolSize) {
&nbsp; &nbsp; &nbsp; &nbsp; if (addWorker(command, true))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;
&nbsp; &nbsp; &nbsp; &nbsp; c = ctl.get();
&nbsp; &nbsp; }
&nbsp; &nbsp; if (isRunning(c) &amp;&amp; workQueue.offer(command)) {
&nbsp; &nbsp; &nbsp; &nbsp; int recheck = ctl.get();
&nbsp; &nbsp; &nbsp; &nbsp; if (! isRunning(recheck) &amp;&amp; remove(command))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reject(command);
&nbsp; &nbsp; &nbsp; &nbsp; else if (workerCountOf(recheck) == 0)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addWorker(null, false);
&nbsp; &nbsp; }
&nbsp; &nbsp; else if (!addWorker(command, false))
&nbsp; &nbsp; &nbsp; &nbsp; reject(command);
}



------------------&nbsp;Original&nbsp;------------------
From:                                                                           
                                             "Tomcat Users List"                
                                                                    
<ch...@christopherschultz.net&gt;;
Date:&nbsp;Tue, Oct 26, 2021 02:33 AM
To:&nbsp;"users"<users@tomcat.apache.org&gt;;

Subject:&nbsp;Re: About the comment of org.apache.tomcat.util.threads.TaskQueue



tianshuang,

On 10/23/21 23:44, Poison wrote:
&gt; Tomcat version: 8.5.72
&gt; 
&gt; 
&gt; org.apache.tomcat.util.threads.TaskQueue&amp;nbsp;source 
code:&amp;nbsp;https://github.com/apache/tomcat/blob/8.5.72/java/org/apache/tomcat/util/threads/TaskQueue.java#L33
&gt; 
&gt; 
&gt; In the comments of the&amp;nbsp;TaskQueue&amp;nbsp;class, it mentions "If 
you use a normal queue, the executor will spawn threads when there are idle 
threads and you wont be able to force items onto the queue 
itself.".&amp;nbsp;But when we use java.util.concurrent.LinkedBlockingQueue, it 
will be put into the queue by default instead of creating threads. What queue 
does the normal queue in the comment refer to?

Just looking at the class definition, I think "normal queue" means "an 
instance of another subclass of BlockingQueue<Runnable&gt;".

The TaskQueue class has a "force" method which does something like 
"offer" but behaves differently. Tomcat can use this internally instead 
of having to only use the more limited "offer" method of the Queue 
interface.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to