| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Tomcat example source code file (

This example Tomcat source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Tomcat tags/keywords

interruptedexception, interruptedexception, masterslaveworkerthread, net, network, object, object, pooltcpendpoint, runnable, socket, socket, string, tcpconnection, tcpconnection, thread, threadwithattributes

The Tomcat source code

 *  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
 *  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.



import org.apache.tomcat.util.threads.ThreadWithAttributes;

 * Regular master slave thread pool. Slave threads will wait for work.
class MasterSlaveWorkerThread implements Runnable {

    protected PoolTcpEndpoint endpoint;
    protected String threadName;
    protected boolean stopped = false;
    private Object threadSync = new Object();
    private Thread thread = null;
    private boolean available = false;
    private Socket socket = null;
    private TcpConnection con = new TcpConnection();
    private Object[] threadData = null;

    public MasterSlaveWorkerThread(PoolTcpEndpoint endpoint, String threadName) {
        this.endpoint = endpoint;
        this.threadName = threadName;

     * Process an incoming TCP/IP connection on the specified socket.  Any
     * exception that occurs during processing must be logged and swallowed.
     * <b>NOTE:  This method is called from our Connector's thread.  We
     * must assign it to our own thread so that multiple simultaneous
     * requests can be handled.
     * @param socket TCP socket to process
    synchronized void assign(Socket socket) {

        // Wait for the Processor to get the previous Socket
        while (available) {
            try {
            } catch (InterruptedException e) {

        // Store the newly available Socket and notify our thread
        this.socket = socket;
        available = true;


     * Await a newly assigned Socket from our Connector, or <code>null
     * if we are supposed to shut down.
    private synchronized Socket await() {

        // Wait for the Connector to provide a new Socket
        while (!available) {
            try {
            } catch (InterruptedException e) {

        // Notify the Connector that we have received this Socket
        Socket socket = this.socket;
        available = false;

        return (socket);


     * The background thread that listens for incoming TCP/IP connections and
     * hands them off to an appropriate processor.
    public void run() {

        // Process requests until we receive a shutdown signal
        while (!stopped) {

            // Wait for the next socket to be assigned
            Socket socket = await();
            if (socket == null)

            // Process the request from this socket
            endpoint.processSocket(socket, con, threadData);

            // Finish up this request


        // Tell threadStop() we have shut ourselves down successfully
        synchronized (threadSync) {


     * Start the background processing thread.
    public void start() {
        threadData = endpoint.getConnectionHandler().init();
        thread = new ThreadWithAttributes(null, this);

     * Stop the background processing thread.
    public void stop() {
        stopped = true;
        thread = null;
        threadData = null;


Other Tomcat examples (source code examples)

Here is a short list of links related to this Tomcat source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.