Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   * 
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * 
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  * 
  * Contributor(s):
  * 
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package com.sun.enterprise.web.connector.grizzly;
 
Internal FIFO used by the Worker Threads to pass information between Task objects.

Author(s):
Jean-Francois Arcand
 
 public class LinkedListPipeline extends LinkedList<Taskimplements Pipeline{


    
The number of thread waiting for a Task
 
     protected int waitingThreads = 0;
    
    
    
The maximum number of Thread
 
     protected int maxThreads = 20;
    

    
The minimum numbers of WorkerThreadImpl
 
     protected int minThreads = 5;
                                                                                
    
The minimum numbers of spare WorkerThreadImpl
 
     protected int minSpareThreads = 2;


    
The port used.
 
     protected int port = 8080;
    

    
The number of WorkerThreadImpl
 
     protected int threadCount = 0;
    

    
The name of this Pipeline
 
     protected String name;
    
    
    
The Thread Priority
 
     protected int priority = .;
    
    
    
Has the pipeline already started
    protected boolean isStarted = false
    

    
WorkerThreadImpl amanged by this pipeline.
    protected transient WorkerThreadImpl[] workerThreads;
    
    
    
Maximum pending connection before refusing requests.
    protected int maxQueueSizeInBytes = -1;
    
    
    
The increment number used when adding new thread.
    protected int threadsIncrement = 1;
    
    
    
The request times out during transaction.
    protected int threadsTimeout = .;
        
    
    
The PipelineStatistic objects used when gathering statistics.
    protected transient PipelineStatistic pipelineStat;
    
    // ------------------------------------------------------- Constructor -----/
    
    public LinkedListPipeline(){
        super();
    }
    
    public LinkedListPipeline(int maxThreadsint minThreadsString name
                              int portint priority){
                        
        this. = maxThreads;
        this. = port;
        this. = name;
        this. = minThreads;
        this. = priority;
        
        if ( minThreads <  )
             = minThreads;
        
    }
    
    public LinkedListPipeline(int maxThreadsint minThreadsString name
                              int port){
                        
        this(maxThreads,minThreads,name,port,.);
    }
   
    // ------------------------------------------------ Lifecycle ------------/
    
    
Init the Pipeline by initializing the required WorkerThreadImpl. Default value is 10
    public synchronized void initPipeline(){
        
        if ( > ) {
             = ;
        }
        
         = new WorkerThreadImpl[];
        increaseWorkerThread(false);        
   }

    
    
Start the Pipeline and all associated WorkerThreadImpl
    public synchronized void startPipeline(){
        if (!) {
            for (int i=0; i < i++){
                [i].start();
            }
             = true;
        }
    }
    

    
Stop the Pipeline and all associated WorkerThreadImpl
    public synchronized void stopPipeline(){
        if () {
            for (int i=0; i < i++){
                [i].terminate();
            }
             = false;
        }
        notifyAll();
    }


    
Create new WorkerThreadImpl. This method must be invoked from a synchronized block.
    protected void increaseWorkerThread(int incrementboolean startThread){        
        WorkerThreadImpl workerThread;
        int currentCount = ;
        int increaseCount =  + increment
        for (int i=currentCounti < increaseCounti++){
            workerThread = new WorkerThreadImpl(this
                     + "WorkerThread-"  +  + "-" + i);
            workerThread.setPriority();
            
            if (startThread)
                workerThread.start();
            
            [i] = workerThread;
            ++; 
        }
    }
    
    
    
Interrupt the Thread using it thread id
    public synchronized boolean interruptThread(long threadID){
        ThreadGroup threadGroup = [0].getThreadGroup();
        Thread[] threads = new Thread[threadGroup.activeCount()];
        threadGroup.enumerate(threads);
        
        for (Thread threadthreads){
            if ( thread != null && thread.getId() == threadID ){                
                if ( .. != thread.getState()){
                    try{
                        thread.interrupt();
                        return true;
                    } catch (Throwable t){
                        SelectorThread.logger().log(.,"",t);
                    }
                }
            }
        }
        return false;
    }
    
    
    // ---------------------------------------------------- Queue ------------//
  
    
    
Add an object to this pipeline
    public synchronized void addTask(Task task) {
        int queueSize =  size();
        if (  != -1 &&  <= queueSize){
            SelectorThread st = task.getSelectorThread();
            st.cancelKey(task.getSelectionKey());
            SelectorThread.logger().log(.,"Maximum connections queued reached " +
                    "(max-pending-count) "
                    +  + 
                    ". Dropping requests until queued connections have been freed up.");
            st.returnTask(task);
            return;
        }
        
        addLast(task);
        notify();
        // Create worker threads if we know we will run out of them
        if ( <  &&  < (queueSize + 1)){
            int left =  - ;
            if ( > left){
                 = left;
            }
            increaseWorkerThread(,true);
        }
    }


    
Return a Task object available in the pipeline. All Threads will synchronize on that method
    public synchronized Task getTask() {
        if (size() -  <= 0) {            
            try { 
                ++; 
                wait();
            }  catch (InterruptedException e)  {
                Thread.currentThread().interrupted();
            }
            --;       
        }
        if ( != null) {
            .gather(size());
        }       
        return poll();
    }

    
    
Invoked when the SelectorThread is about to expire a SelectionKey.

Returns:
true if the SelectorThread should expire the SelectionKey, false if not.
    public boolean expireKey(SelectionKey key){
       return true
    }    
    
    
    
Return true if the size of this ArrayList minus the current waiting threads is lower than zero.
    public boolean isEmpty() {
        return  (size() -  <= 0);
    }
    
    // --------------------------------------------------Properties ----------//

    
Return the number of waiting threads.
    public synchronized int getWaitingThread(){
        return ;
    }
    
    
    
Set the number of threads used by this pipeline.
    public synchronized void setMaxThreads(int maxThreads){
        this. = maxThreads;
    }    
    
    
    
Return the number of threads used by this pipeline.
    public synchronized int getMaxThreads(){
        return ;
    }
    
    
    public synchronized int getCurrentThreadCount() {
        return ;
    }
      
      
    
Return the curent number of threads that are currently processing a task.
    public synchronized int getCurrentThreadsBusy(){
        return ( - );
    }
        

    
Return the maximum spare thread.
    public synchronized int getMaxSpareThreads() {
        return ;
    }


    
Return the minimum spare thread.
    public synchronized int getMinSpareThreads() {
        return ;
    }


    
Set the minimum space thread this Pipeline can handle.
    public synchronized void setMinSpareThreads(int minSpareThreads) {
        this. = minSpareThreads;
    }

    
    
Set the thread priority of the Pipeline
    public synchronized void setPriority(int priority){
        this. = priority;
    }
    
    
    
Set the name of this Pipeline
    public synchronized void setName(String name){
        this. = name;
    }
    
    
    
Return the name of this Pipeline

Returns:
the name of this Pipeline
    public synchronized String getName(){
        return +;
    }    

    
    
Set the port used by this Pipeline

Parameters:
port the port used by this Pipeline
    public synchronized void setPort(int port){
        this. = port;
    }
    
    
    
Set the minimum thread this Pipeline will creates when initializing.

Parameters:
minThreads the minimum number of threads.
    public synchronized void setMinThreads(int minThreads){
        this. = minThreads;
    }
    
    
    public String toString(){
       return "name: " +  + " maxThreads: " +  
                + " type: " + this.getClass().getName();        
    }
    
    
    
Set the number the Pipeline will use when increasing the thread pool
    
    public synchronized void setThreadsIncrement(int threadsIncrement){
        this. = threadsIncrement;
    }
    
    
    
Set the timeout value a thread will use to times out the request.
   
    public synchronized void setThreadsTimeout(int threadsTimeout){
        this. = threadsTimeout;
    }
    
    
    
The number of Task currently queued

Returns:
number of queued connections
    public synchronized int getTaskQueuedCount(){
       return size();
    }

    
    
Set the maximum pending connection this Pipeline can handle.
    public synchronized void setQueueSizeInBytes(int maxQueueSizeInBytesCount){
        this. = maxQueueSizeInBytesCount;
    }
    
    
    
Get the maximum pending connection this Pipeline can handle.
    public synchronized int getQueueSizeInBytes(){
        return ;
    }   
  
    
    
Set the PipelineStatistic object used to gather statistic;
    public void setPipelineStatistic(PipelineStatistic pipelineStatistic){
        this. = pipelineStatistic;
    }
    
    
    
Return the PipelineStatistic object used to gather statistic;
        return ;
    }
    
New to GrepCode? Check out our FAQ X