com.sun.jini.thread
Class ThreadPool

java.lang.Object
  extended by com.sun.jini.thread.ThreadPool
All Implemented Interfaces:
Executor

final class ThreadPool
extends Object
implements Executor

ThreadPool is a simple thread pool implementation of the Executor interface. A new task is always given to an idle thread, if one is available; otherwise, a new thread is always created. There is no minimum warm thread count, nor is there a maximum thread count (tasks are never queued unless there are sufficient idle threads to execute them). New threads are created as daemon threads in the thread group that was passed to the ThreadPool instance's constructor. Each thread's name is the prefix NewThreadAction.NAME_PREFIX followed by the name of the task it is currently executing, or "Idle" if it is currently idle.

This implementation uses the Logger named com.sun.jini.thread.ThreadPool to log information at the following levels:

Level Description
WARNING uncaught exception in worker thread

Author:
Sun Microsystems, Inc.

Nested Class Summary
private static class ThreadPool.Task
          Task simply encapsulates a task's Runnable object with its name.
private  class ThreadPool.Worker
          Worker executes an initial task, and then it executes tasks from the queue, passing away if ever idle for more than the idle timeout value.
 
Field Summary
private  int idleThreads
          threads definitely available to take new tasks
private static long idleTimeout
          how long a thread waits in the idle state before passing away
private  Object lock
          lock guarding all mutable instance state (below)
private static Logger logger
           
private  LinkedList queue
          queues of tasks to execute
private  ThreadGroup threadGroup
          thread group that this pool's threads execute in
 
Constructor Summary
ThreadPool(ThreadGroup threadGroup)
          Creates a new thread group that executes tasks in threads of the given thread group.
 
Method Summary
 void execute(Runnable runnable, String name)
          Executes the given Runnable action asynchronously in some thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

idleTimeout

private static final long idleTimeout
how long a thread waits in the idle state before passing away


logger

private static final Logger logger

threadGroup

private final ThreadGroup threadGroup
thread group that this pool's threads execute in


lock

private final Object lock
lock guarding all mutable instance state (below)


idleThreads

private int idleThreads
threads definitely available to take new tasks


queue

private final LinkedList queue
queues of tasks to execute

Constructor Detail

ThreadPool

ThreadPool(ThreadGroup threadGroup)
Creates a new thread group that executes tasks in threads of the given thread group.

Method Detail

execute

public void execute(Runnable runnable,
                    String name)
Description copied from interface: Executor
Executes the given Runnable action asynchronously in some thread. The implementation may create a new thread to execute the action, or it may execute the action in an existing thread. The execution of a given action must not be delayed indefinitely in order to complete execution of a different action passed to a different invocation of this method. In other words, the implementation must assume that there may be arbitrary dependencies between actions passed to this method, so it needs to be careful to avoid potential deadlock by delaying execution of one action indefinitely until another completes. Also, this method itself must not block, because it may be invoked by code that is serially processing data to produce multiple such arbitrarily-dependent actions that need to be executed.

Specified by:
execute in interface Executor
Parameters:
runnable - the Runnable action to execute
name - string to include in the name of the thread used to execute the action


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.