Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. 
   * See the copyright.txt in the distribution for a full listing 
   * of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  * 
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 package com.arjuna.services.framework.task ;
 
 import java.util.Set;
 
This class manages the client side of the task manager

Author(s):
kevin
 
 public class TaskManager
 {
    
The singleton.
 
     private static final TaskManager MANAGER = new TaskManager() ;

    
The default maximum worker count.
 
     private static final int DEFAULT_MAXIMUM_THREAD_COUNT = 10 ;

    
The default minimum worker count.
 
     private static final int DEFAULT_MINIMUM_THREAD_COUNT = 0 ;

    
The minimum worker pool count.
 
     private int minimumWorkerCount =  ;

    
The maximum worker pool count.
 
     private int maximumWorkerCount =  ;

    
The set of already allocated workers.
 
     private Set workerPool = new HashSet() ;

    
The current list of tasks.
 
     private LinkedList taskList = new LinkedList() ;

    
The counter used for naming the threads.
 
     private int taskCount ;

    
The number of worker waiting.
 
     private int waitingCount ;

    
A flag indicating that shutdown is in progress.
 
     private boolean shutdown ;

    
Get the singleton controlling the tasks.

Returns:
The task manager.
 
     public static TaskManager getManager()
     {
         return  ;
     }

    
Private to prevent initialisation.
 
    private TaskManager()
    {
    }

    
Queue the task for execution.

Parameters:
task The task to be executed.
Returns:
true if the task was queued, false otherwise.
    public boolean queueTask(final Task task)
    {
        final boolean debugEnabled = ..isTraceEnabled() ;
        synchronized()
        {
            if ()
            {
                if (debugEnabled)
                {
                    ..tracev("Shutdown in progress, ignoring task") ;
                }
                return false ;
            }
        }
        final boolean notify ;
        synchronized()
        {
            .addLast(task) ;
            notify = ( > 0) ;
            if (notify)
            {
                if (debugEnabled)
                {
                    ..tracev("queueTask: notifying waiting workers ({0})",
                            new Object[] {new Integer()}) ;
                }
                .notify() ;
            }
        }
        final boolean create ;
        synchronized()
        {
            create = ((.size() < ) ||
                    ((.size() < ) && !notify)) ;
        }
        if (create)
        {
            if (debugEnabled)
            {
                ..tracev("queueTask: creating worker") ;
            }
            createWorker() ;
        }
        else if (debugEnabled)
        {
            ..tracev("queueTask: queueing task for execution") ;
        }
        
        return true ;
    }

    
Set the minimum worker count for the pool.

Parameters:
minimumWorkerCount The minimum worker count.
    public void setMinimumWorkerCount(final int minimumWorkerCount)
    {
        final boolean debugEnabled = ..isTraceEnabled() ;
        synchronized()
        {
            if ()
            {
                if (debugEnabled)
                {
                    ..tracev("shutdown in progress, ignoring set minimum worker count") ;
                }
                return ;
            }
            this. = (minimumWorkerCount < 0 ? 
                    : minimumWorkerCount) ;
            if (this. > )
            {
                 = this. ;
            }
            if (debugEnabled)
            {
                ..tracev("setMinimumWorkerCount: {0}") ;
            }
        }
        while(true)
        {
            final boolean create ;
            synchronized()
            {
                create = (.size() < this.) ;
            }
            if (create)
            {
                createWorker() ;
            }
            else
            {
                break ;
            }
        }
    }

    
Get the minimum worker count for the pool.

Returns:
The minimum worker count.
    public int getMinimumWorkerCount()
    {
        synchronized()
        {
            return  ;
        }
    }

    
Set the maximum worker count for the pool.

Parameters:
maximumWorkerCount The maximum worker count.
    public void setMaximumWorkerCount(final int maximumWorkerCount)
    {
        final boolean debugEnabled = ..isTraceEnabled() ;
        synchronized()
        {
            if ()
            {
                if (debugEnabled)
                {
                    ..tracev("shutdown in progress, ignoring set maximum worker count") ;
                }
                return ;
            }
            this. = (maximumWorkerCount < 0 ? 
                    : maximumWorkerCount) ;
            if ( > this.)
            {
                 = this. ;
            }
            if (debugEnabled)
            {
                ..tracev("setMaximumWorkerCount: {0}",
                        new Object[] {new Integer(this.)}) ;
            }
            synchronized()
            {
                if ((.size() > this.)
                        && ( > 0))
                {
                    if (debugEnabled)
                    {
                        ..tracev("setMaximumWorkerCount: reducing pool size from {0} to {1}",
                                new Object[] {new Integer(.size()), new Integer(this.)}) ;
                    }
                    .notify() ;
                }
            }
        }
    }

    
Get the maximum worker count for the pool.

Returns:
The maximum worker count.
    public int getMaximumWorkerCount()
    {
        synchronized()
        {
            return  ;
        }
    }

    
Get the current worker count for the pool.

Returns:
The current worker count.
    public int getWorkerCount()
    {
        synchronized()
        {
            return .size() ;
        }
    }

    
Close all threads and reset the task list. This method waits until all threads have finished before returning.
    public void shutdown()
    {
        final boolean debugEnabled = ..isTraceEnabled() ;
        synchronized()
        {
            if ()
            {
                if (debugEnabled)
                {
                    ..tracev("Shutdown already in progress") ;
                }
            }
            else
            {
                setMaximumWorkerCount(0) ;
                 = true ;
            }
        }
        while(true)
        {
            final Thread waitThread ;
            synchronized()
            {
                final Iterator workerPoolIter = .iterator() ;
                if (workerPoolIter.hasNext())
                {
                    waitThread = (ThreadworkerPoolIter.next() ;
                }
                else
                {
                    waitThread = null ;
                }
            }
            if (waitThread == null)
            {
                break ;
            }
            else
            {
                try
                {
                    waitThread.join() ;
                }
                catch (final InterruptedException ie)
                {
                } // Ignore
            }
        }
        synchronized()
        {
            if ()
            {
                .clear() ;
                 = false ;
            }
        }
    }

    
Get another task from the pool.

Returns:
The next task from the pool or null if finished.
    Task getTask()
    {
        final boolean debugEnabled = ..isTraceEnabled() ;
        while(true)
        {
            final boolean remove ;
            synchronized()
            {
                final int excessCount = .size() -  ;
                if (excessCount > 0)
                {
                    if (debugEnabled)
                    {
                        ..tracev("getTask: releasing thread") ;
                    }
                    synchronized()
                    {
                        if ((excessCount > 1) && ( > 0))
                        {
                            if (debugEnabled)
                            {
                                ..tracev("getTask: notifying waiting thread about excess count {0}",
                                        new Object[] {new Integer(excessCount)}) ;
                            }
                            .notify() ;
                        }
                    }
                    remove = true ;
                }
                else
                {
                    remove = false ;
                }
            }
            if (remove)
            {
                final Thread currentThread = Thread.currentThread() ;
                synchronized()
                {
                    .remove(currentThread) ;
                }
                return null ;
            }
            synchronized()
            {
                final int numTasks = .size() ;
                if (numTasks > 0)
                {
                    final Task task = (Task.removeFirst() ;
                    if ((numTasks > 1) && ( > 0))
                    {
                        .notify() ;
                    }
                    if (debugEnabled)
                    {
                        ..tracev("getTask: returning task") ;
                    }
                    return task ;
                }
                ++ ;
                if (debugEnabled)
                {
                    ..tracev("getTask: waiting for task") ;
                }
                try
                {
                    .wait() ;
                }
                catch (final InterruptedException ie)
                {
                    if (debugEnabled)
                    {
                        ..tracev("getTask: interrupted") ;
                    }
                }
                finally
                {
                    -- ;
                }
            }
        }
    }

    
Create and register a task worker.
    private void createWorker()
    {
        final TaskWorker taskWorker = new TaskWorker(this) ;
        final String name ;
        synchronized()
        {
            name = "TaskWorker-" + ++ ;
        }
        final Thread thread = new Thread(taskWorkername) ;
        thread.setDaemon(true) ;
        synchronized()
        {
            .add(thread) ;
        }
        thread.start() ;
    }
New to GrepCode? Check out our FAQ X