5.常用方法: public void run(){}:如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。<所以该方法应自觉重写!!!> public void start(){}:使该线程开始执行;Java 虚拟机调用该线程的 run 方法 public final String getName(){}:返回线程的名称 public final int getPriority(){}:返回线程优先级 public final void setName(String name){}:设定线程名称 public final void setPriority(int newPriority){}:设定线程的优先值 public final ThreadGroup getThreadGroup(){}:Returns the thread group to which this thread belongs. This method returns null if this thread has died (been stopped). 代码演示:
六.ThreadGroup 类(java.lang) 简介:线程组:java允许对一批线程进行管理,使用ThreadGroup表示线程组,所有线程均有指定线程组,如果没有显式指定,则为默认线程组.默认情况下,子线程和创建他的父线程 属于同一线程组.一旦某线程加入指定线程组内,该线程会一直属于该组,直至死亡,运行过程中不可改变. 继承关系:java.lang.Object--java.lang.ThreadGroup 定义:public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler 构造器: ThreadGroup(String name): Constructs a new thread group. ThreadGroup(ThreadGroup parent, String name): Creates a new thread group. 常用方法: public int activeCount(){}:Returns an estimate of the number of active threads in this thread group and its subgroups public int activeGroupCount(){}:Returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group. public int enumerate(Thread[] list) Throws SecurityException{}: public int enumerate(Thread[] list,boolean recurse)Throws SecurityException{}:Copies into the specified array every active thread in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread in these subgroups are also included. If the array is too short to hold all the threads, the extra threads are silently ignored. public final String getName(){}:Returns the name of this thread group. public final ThreadGroup getParent()Throws SecurityException{}:Returns the parent of this thread group. public final boolean isDaemon(){}:Tests if this thread group is a daemon thread group public final void checkAccess() Throws SecurityException{}:Determines if the currently running thread has permission to modify this thread group. public final void setDaemon(boolean daemon)Throws SecurityException{}:Changes the daemon status of this thread group.
七.Executor 接口(java.io.concurrent) 简介:线程池:由于线程涉及到与操作系统交互,所以启动一个新线程的成本比较高,因此,java提供了线程池机制来提高性能,尤其是当程序中需要大量生存期很短暂的线程时,应该考虑 使用线程池.所谓线程池是指在系统启动时即创建大量空闲的线程,程序将一个Runnable对象或Callable对象传给线程池,线程池就会启动一个线程来执行他们的run或call方法,当方法 结束时,线程并不会死亡,而是返回线程池成为空闲状态,等待执行下一个任务 定义: public interface Executor 方法:public void execute(Runnable command) Throws RejectedExecutionException | NullPointerException {}:Executes the given command at some time in the future 实现类:AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor 子接口:ExecutorService, ScheduledExecutorService