Thread Objects
Each thread is associated with an instance of the class Thread
. There are two basic strategies for using Thread
objects to create a concurrent application.
- To directly control thread creation and management, simply instantiate
Thread
each time the application needs to initiate an asynchronous task. - To abstract thread management from the rest of your application, pass the application's tasks to an executor.
This section documents the use of Thread
objects. Executors are discussed with other high-level concurrency objects.
- 为了直接控制和管理线程,可以在应用程序每次需要异步执行任务的时候简单的实现Threa对象即可。
- 为了从应用程序的其他地方抽象的管理线程,可以传递应用程序的任务到executor。
这个课程描述了Thread对象的使用。关于executors的讨论放在了其他high-level concurrency 对象种。
Defining and Starting a Thread
An application that creates an instance of Thread
must provide the code that will run in that thread. There are two ways to do this:
- Provide a
Runnable
object. TheRunnable
interface defines a single method,run
, meant to contain the code executed in the thread. TheRunnable
object is passed to theThread
constructor, as in the
example:HelloRunnable
public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }
- Subclass
Thread
. TheThread
class itself implementsRunnable
, though itsrun
method does nothing. An application can subclassThread
, providing its own implementation ofrun
, as in the
example:HelloThread
public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
Notice that both examples invoke Thread.start
in order to start the new thread.
Which of these idioms should you use? The first idiom, which employs a Runnable
object, is more general, because theRunnable
object can subclass a class other than Thread
. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread
. This lesson focuses on the first approach, which separates the Runnable
task from the Thread
object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.
The Thread
class defines a number of methods useful for thread management. These include static
methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread
object. We'll examine some of these methods in the following sections.
译文:
定义和开始线程
一个应用程序创建一个Thread的实例必须提供能在线程中运行的代码。这里有两种方法做这件事情:
- 提供一个Runnable对象,这个Runnable接口定义了单个方法,run方法,包含能够在线程中执行的代码。正如在HelloRunnable的实例中一样,这个Runnable对象通过Thread的构造方法传递。
1 public class HelloRunnable implements Runnable { 2 3 public void run() { 4 System.out.println("Hello from a thread!"); 5 } 6 7 public static void main(String args[]) { 8 (new Thread(new HelloRunnable())).start(); 9 } 10 }
- 实现Thread的子类。Thread类通过一个不包括任何东西的run方法实现了Runnable.如同HelloThread实例,一个应用程序可以继承自Thread,通过重写run方法来实现多线程。
1 public class HelloThread extends Thread { 2 3 public void run() { 4 System.out.println("Hello from a thread!"); 5 } 6 7 public static void main(String args[]) { 8 (new HelloThread()).start(); 9 } 10 }
注意:两种方法都调用了start方法来启动这个线程。
这里是否有什么约定俗称的规则了?第一个规则,用runnable对象的方法,更加的通用。因为runnable对象不仅能实现thread类,而且能实现一个子类。第二个是更加容易在简单的程序中使用,但是,有一个事实就是你的任务类必须是Thread类的子类。这个课程主要集中于第一中实现方法,这个方法使得runnable对象的实现和thread对象的执行分开。不仅仅是因为这种方法更加的灵活,也因为它对一个讨论的高性能的并发也是兼容的。
Thread类定义了大量对线程管理有用的方法。其中包括静态方法,它提供的信息或者影响的状态,线程执行这个方法。被其他的线程执行管理线程或者线程对象的涉及到其他的方法。我们将会在接下来的课程中介绍这些方法。