站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Java Threads, Third Edition

10.5 Thread Creation - Java Threads, Third Edition

Previous Section  < Day Day Up >  Next Section

10.5 Thread Creation

The thread pool dynamically creates threads according to the size policies in effect when a task is queued and terminates threads when they've been idle too long. Those policies are set when the pool is constructed, and they can be altered with these methods:

package java.util.concurrent;

public interface ThreadFactory {

    public Thread newThread(Runnable r);

}



package java.util.concurrent;

public class ThreadPoolExecutor implements ExecutorService {

    public void setThreadFactory(ThreadFactory threadFactory);

    public ThreadFactory getThreadFactory( );

    public void setKeepAliveTime(long time, TimeUnit unit);

    public long getKeepAliveTime(TimeUnit unit);

}

When the pool creates a thread, it uses the currently installed thread pool factory to do so. Creating and installing your own thread factory allows you to set up a custom scheme to create threads so that they are created with special names, priorities, daemon status, thread group, and so on.

The default thread factory creates a thread with the following characteristics:

  • New threads belong to the same thread group as the thread that created the executor. However, the security manager policy can override this and place the new thread in its own thread group (see Chapter 13).

  • The name of the thread reflects its pool number and its thread number within the pool. Within a pool, threads are numbered consecutively beginning with 1; thread pools are globally assigned a pool number consecutively beginning with 1.

  • The daemon status of the thread is the same as the status of the thread that created the executor.

  • The priority of the thread is Thread.NORM_PRIORITY.

    Previous Section  < Day Day Up >  Next Section