Thread

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.

一个thread指的是程序当中执行的一个线程。java虚拟机允许一个应用拥有多个并发执行的线程。每个线程都会有一个优先级。拥有更高优先级的线程他们执行的情况要优先于优先级低的线程。每个线程可以也可以不被标记为一个后台线程。运行在一些线程中的代码创建了一个新的线程对象,这个新的线程对象的优先级被设置为与创建它的线程的优先级相同,并且只有当创建它的线程为后台线程的话被创建的线程才是后台线程。

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虚拟机启动时,通常会有一个非后台线程(这个线程通常会调用所指定类的一个main方法),java虚拟机会继续的去执行这个线程直到下面这两种情况发生:

  • 类Runtime的exit方法被调用,并且安全管理器允许推出操作发生。
  • 所有的非后台线程已经消亡了,消亡的原因要么是通过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的子类。这个子类应该重写Thead的run方法。子类的实例接下来可以被分配去启动。比如一个线程去计算大于指定值的质数可以用如下写法:

1
2
3
4
5
6
7
8
9
10
11
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:

以下代码将创建一个线程并且启动它:

1
2
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:

另外一种方法创建一个线程是申明一个类实现Runable接口。然后实现run方法。子类的实例接下来可以被分配,当创建线程时做一个参数传递并且去启动。同样的例子用另外一种风格看起来如下:

1
2
3
4
5
6
7
8
9
10
11
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:

以下代码将创建一个线程并且启动它:

1
2
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.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

每一个线程都有一个名字用于表示目的。有多个线程可以有相同的名字。当一个线程被创建的时候如果没有去指定名字,那么一个新的名字将会被其产生出来。除非做了其他说明,否则将一个null参数传递给构造方法或者是这个类的其他方法里将会导致一个空指针异常

1
public synchronized void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

调用了start方法后导致这个线程开始执行;java虚拟机会调用线程的run方法。

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

结果是两个线程会并发的运行:当前线程(从对于start调用返回来的线程)以及另外线程(执行run方法的调用)

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

启动一个线程超过一次是不合法的。特别的,一个线程不能被重新启动一旦它已经执行完成之后。

1
2
3
4
5
6
@Override
public void run() {
if (target != null) {
target.run();
}
}

If this thread was constructed using a separate Runnable run object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.

如果这个线程是通过单独的Runnable对象来去构建的,那么Runnable对象的run方法将被调用;否则这个方法是什么都不做直接返回的。
线程的子类必须重写这个方法。

Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

Runnable接口应该被任何一个实例打算被线程执行的类所实现。这个类必须要定义一个无参的名字是run的方法。

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

这个接口被设计成提供一个公用的协议针对于当他们激活的时候希望去执行代码的对象。比如说,Runnable被Thread实现出来了。处于激活状态只是简单的表示一个线程被启动了但是还尚未被停止。

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

此外,Runnable提供了一种方式一个类被激活同时没有继承Thread。一个类实现了Runnable接口可以不用继承Thread类而是通过实例化一个线程实例然后将它自己作为目标传进去,大多数时候应当使用Runnable在你只打算重写run方法并不打算重写Tread类的其他方法的时候。这是重要的因为除非程序员打算去修改或者增强这个类的一些基础行为否则类是不应该被子类化的。

1
public abstract void run()

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object’s run method to be called in that separately executing thread.
The general contract of the method run is that it may take any action whatsoever.

当一个对象实现Runnable接口被用作创建一个线程时,启动线程会导致对象的run方法将在那个独立的执行线程中被调用。
方法run的一种通用契约就是它可以接受任何动作。