这两天笔者几篇文章介绍了改线程进程的文章. 关联文章的地址
第一次翻译,从APi开始。。。
java.lang
Class Thread
java.lang.Object
java.lang.Thread
extends implements
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 class Runtime
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 the run
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 |
|
|
|
|
|
|
|
|
每日一道理
如果你们是蓝天,我愿做衬托的白云;如果你们是鲜花,我愿做陪伴的小草;如果你们是大树,我愿做点缀的绿叶……我真诚地希望我能成为你生活中一个欢乐的音符,为你的每一分钟带去祝福。
Method Summary | |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
法方真多,终于弄完了。
dd368fc7d444 8 月前
8ab964a5e453 8 月前