Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.sun.jini.thread
Class TaskManager

    java.lang.Object
      com.sun.jini.thread.TaskManager

public class TaskManager
extends Object

A task manager manages a single queue of tasks, and some number of worker threads. New tasks are added to the tail of the queue. Each thread loops, taking a task from the queue and running it. Each thread looks for a task by starting at the head of the queue and taking the first task (that is not already being worked on) that is not required to run after any of the tasks that precede it in the queue (including tasks that are currently being worked on).

This class uses the `Logger` named `com.sun.jini.thread.TaskManager` to log information at the following logging levels:

`com.sun.jini.thread.TaskManager`
Level | Description
--- | ---
`INFO` | exceptions thrown by `TaskManager.Task` methods

Nested Class Summary
`static interface` | `TaskManager.Task` The interface that tasks must implement

Field Summary
`protected int` | `firstPending` Index of the first pending task; all earlier tasks are active
`protected float` | `loadFactor` Threshold for creating new threads
`protected static Logger` | `logger` Logger
`protected int` | `maxThreads` Maximum number of threads allowed
`protected List` | `roTasks` Read-only view of tasks
`protected ArrayList` | `tasks` Active and pending tasks
`protected boolean` | `terminated` True if manager has been terminated
`protected List` | `threads` Active threads
`protected long` | `timeout` Idle time before a thread should exit

Constructor Summary
`TaskManager()` Create a task manager with maxThreads = 10, timeout = 15 seconds, and loadFactor = 3.0.
`TaskManager(int maxThreads, long timeout, float loadFactor)` Create a task manager.

Method Summary
`void` | `add(TaskManager.Task t)` Add a new task.
`void` | `addAll(Collection c)` Add all tasks in a collection, in iterator order.
`void` | `addIfNew(TaskManager.Task t)` Add a new task if it is not equal to (using the equals method) to any existing active or pending task.
`int` | `getMaxThreads()` Return the maximum number of threads to use on tasks.
`ArrayList` | `getPending()` Return all pending tasks.
`protected boolean` | `needThread()` Return true if a new thread should be created (ignoring maxThreads).
`boolean` | `remove(TaskManager.Task t)`
`boolean` | `removeIfPending(TaskManager.Task t)` Remove a task if it is pending (not active).
`void` | `terminate()` Interrupt all threads, and stop processing tasks.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### logger

    protected static final Logger logger

  Logger

### tasks

    protected final ArrayList tasks

  Active and pending tasks

### firstPending

    protected int firstPending

  Index of the first pending task; all earlier tasks are active

### roTasks

    protected final List roTasks

  Read-only view of tasks

### threads

    protected final List threads

  Active threads

### maxThreads

    protected final int maxThreads

  Maximum number of threads allowed

### timeout

    protected final long timeout

  Idle time before a thread should exit

### loadFactor

    protected final float loadFactor

  Threshold for creating new threads

### terminated

    protected boolean terminated

  True if manager has been terminated

Constructor Detail

### TaskManager

    public TaskManager()

  Create a task manager with maxThreads = 10, timeout = 15 seconds, and loadFactor = 3.0.

### TaskManager

    public TaskManager(int maxThreads,
                       long timeout,
                       float loadFactor)

  Create a task manager.

Parameters:
  `maxThreads` - maximum number of threads to use on tasks
  `timeout` - idle time before a thread exits
  `loadFactor` - threshold for creating new threads. A new thread is created if the total number of runnable tasks (both active and pending) exceeds the number of threads times the loadFactor, and the maximum number of threads has not been reached.

Method Detail

### addIfNew

    public void addIfNew(TaskManager.Task t)

  Add a new task if it is not equal to (using the equals method) to any existing active or pending task.

### add

    public void add(TaskManager.Task t)

  Add a new task.

### addAll

    public void addAll(Collection c)

  Add all tasks in a collection, in iterator order.

### needThread

    protected boolean needThread()

  Return true if a new thread should be created (ignoring maxThreads).

### removeIfPending

    public boolean removeIfPending(TaskManager.Task t)

  Remove a task if it is pending (not active). Object identity (==) is used, not the equals method. Returns true if the task was removed.

### remove

    public boolean remove(TaskManager.Task t)

### terminate

    public void terminate()

  Interrupt all threads, and stop processing tasks. Only getPending should be used afterwards.

### getPending

    public ArrayList getPending()

  Return all pending tasks. A new list is returned each time.

### getMaxThreads

    public int getMaxThreads()

  Return the maximum number of threads to use on tasks.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2004 Sun Microsystems, Inc. All rights reserved