线程进程Java API:Thread
这两天笔者几篇文章介绍了改线程进程的文章. 关联文章的地址
第一次翻译,从APi开始。。。
java.lang
Class Thread
java.lang.Object
java.lang.Thread
- All Implemented Interfaces:
- Runnable
public class Thread
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虚拟机让一个应用有拥多个线程且可以同时执行。
线程有优先级。优先级高的线程先于优先级低的线程执行。某个线程可能会被标识为保卫进程。当在线程里建创一个新的线程对象,新线程的优先级最初设定为建创线程的优先级,当且仅当建创线程是保卫进程,则新线程也是保卫进程。
当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:
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
有两种法方来建创新线程。第一种法方是声明一个类为Thread类的子类,这个子类必须重载Thread类的run法方(
这里难道不是重写么
。。),这之后子类的实例才能分配到内存并开始执行。举个栗子,一个线程计算大于规定值的素数可以这样写。。。
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:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
建创线程的第二种法方是声明一个类实现Runnable接口。这个类也会实现run法方。类的实例能被分配,也能在线程建创时被当作参数传递。一个栗子是这样滴。。。
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() 使当前线程暂时停顿并答应其他线程执行。 |
法方真多,终于弄完了。
文章结束给大家分享下程序员的一些笑话语录:
一程序员告老还乡,想安度晚年,于是决定在书法上有所造诣。省略数字……,准备好文房4宝,挥起毛笔在白纸上郑重的写下:Hello World