第一次翻译,从APi开始。。。
java.lang
Class Thread
java.lang.Objectjava.lang.Thread
-
All Implemented Interfaces:
- Runnable
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread
object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main
of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
- The
exit
method of classRuntime
has been called and the security manager has permitted the exit operation to take place. - All threads that are not daemon threads have died, either by returning from the call to the
run
method or by throwing an exception that propagates beyond therun
method.
线程有优先级。优先级高的线程先于优先级低的线程执行。某个线程可能会被标识为守护进程。当在线程里创建一个新的线程对象,新线程的优先级最初设定为创建线程的优先级,当且仅当创建线程是守护进程,则新线程也是守护进程。
当Java虚拟机启动,通常会启动一个非守护进程(在一些类中它是main方法).Java虚拟机继续执行线程直到以下两种情况发生:
1 Runtime类中的exit方法被调用,并且安全管理允许这样的操作。
2 所有非守护进程的线程都停止,可能是run方法的返回,也可能是抛出异常传播到run方法。
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread
. This subclass should override the run
method of class Thread
. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:
有两种方法来创建新线程。第一种方法是声明一个类为Thread类的子类,这个子类必须重载Thread类的run方法( 这里难道不是重写么。。),这之后子类的实例才能分配到内存并开始执行。举个栗子,一个线程计算大于规定值的素数可以这样写。。。class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
The following code would then create a thread and start it running:
上述代码可以创建一个线程并开始执行。PrimeThread p = new PrimeThread(143); p.start();
The other way to create a thread is to declare a class that implements the Runnable
interface. That class then implements the run
method. An instance of the class can then be allocated, passed as an argument when creating Thread
, and started. The same example in this other style looks like the following:
创建线程的第二种方法是声明一个类实现Runnable接口。这个类也会实现run方法。类的实例能被分配,也能在线程创建时被当作参数传递。一个栗子是这样滴。。。class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
The following code would then create a thread and start it running:
上述代码可以创建一个线程执行,两种不同的创建方法调用的方法都是一样。PrimeRun p = new PrimeRun(143); new Thread(p).start();
Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
每个线程都有标识目的的名字。多个线程可能有相同的名字。当线程创建时没有为其指定一个名字,那么将生成一个新名字。
Constructor Summary | |
---|---|
Thread() Allocates a new Thread object. | |
Thread(Runnable target) Allocates a new Thread object. | |
Thread(Runnable target, String name) Allocates a new Thread object. | |
Thread(String name) Allocates a new Thread object. | |
Thread(ThreadGroup group, Runnable target) Allocates a new Thread object. | |
Thread(ThreadGroup group, Runnable target, String name) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group . | |
Thread(ThreadGroup group, Runnable target, String name, long stackSize) Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group , and has the specified stack size. | |
Thread(ThreadGroup group, String name) Allocates a new Thread object. |
Method Summary | |
---|---|
static int | activeCount() 返回当前线程所在线程组的活跃线程数目。 |
void | checkAccess() 确定当前运行线程是否运行修改。 |
protected Object | clone() 如果当前对象的类是可复制的,返回一个对象的克隆。 |
int | countStackFrames() Deprecated. |
static Thread | currentThread() 返回当前正在执行的线程对象的引用。 |
void | destroy() Deprecated. |
static void | dumpStack() 打印当前线程到标准错误流的堆栈。 |
static int | enumerate(Thread[] tarray) 把当前线程组和子组中的所有活跃线程复制到指定数组。 |
static Map<Thread,StackTraceElement[]> | getAllStackTraces() 返回所有活动线程的堆栈。 |
ClassLoader | getContextClassLoader() 返回该进程的上下文类加载器。 |
static Thread.UncaughtExceptionHandler | getDefaultUncaughtExceptionHandler() 返回线程因为异常终止时的默认处理Handler。 |
long | getId() 返回线程的标识号。 |
String | getName() 返回线程的名字。 |
int | getPriority() 返回线程的优先级。 |
StackTraceElement[] | getStackTrace() 。。。 |
Thread.State | getState() 返回线程状态 |
ThreadGroup | getThreadGroup() 返回线程所在的线程组。 |
Thread.UncaughtExceptionHandler | getUncaughtExceptionHandler() 返回线程因为异常终止时的处理Handler,非static。 |
static boolean | holdsLock(Object obj) 如果当前线程拥有指定对象上的监视锁时返回true。 |
void | interrupt() 中断线程。 |
static boolean | interrupted() 测试当前线程有没有被中断。 |
boolean | isAlive() 测试当前线程是否活跃。 |
boolean | isDaemon() 测试当前线程是否是守护线程 |
boolean | isInterrupted() 测试当前线程有没有被中断,非static。 |
void | join() 等待线程结束后再执行join之后的代码。 |
void | join(long millis) 。。。 |
void | join(long millis, int nanos) 。。。 |
void | resume() Deprecated. |
void | run() 。。。 |
void | setContextClassLoader(ClassLoader cl) 设置该线程的上下文类加载器。 |
void | setDaemon(boolean on) 标识该线程是一个守护线程还是用户线程。 |
static void | setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) 当线程因为异常中止时为线程设置默认处理handler。 |
void | setName(String name) 设置线程名称。 |
void | setPriority(int newPriority) 设置线程优先级。 |
void | setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) 当线程因为异常中止时为线程设置默认处理handler,非static。 |
static void | sleep(long millis) 使当前进程停止运行的毫秒数。 |
static void | sleep(long millis, int nanos) 。。。 |
void | start() 启动线程,JVM会调用run方法。 |
void | stop() Deprecated. |
void | stop(Throwable obj) Deprecated. |
void | suspend() Deprecated. |
String | toString() 返回线程的字符串表现形式,包括线程的名称、优先级和线程组。 |
static void | yield() 使当前线程暂时停顿并允许其他线程执行。 |
方法真多,终于弄完了。