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.
这节主要记载了线程对象的使用。执行者会与其他高级的同步对象一起讨论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:应用程序创建一个线程的实例必须提供将要在这个线程中运行的代码。有2种方法实现这个。Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example: 提供一个Runnable对象。Runnable接口只定义了一个方法run,这个run方法中包含了在线程中要执行的代码。Runnable对象被传给Thread类的构造器,和这个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. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example: 继承Thread。Thread类实现了Runnable接口,尽管它的run方法什么也没干。应用程序可以继承Thread类,提供他自己的run方法实现。就像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.主要上面2个例子都是调用Thread.start类启动一个新线程Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable 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.你将会使用哪种方式呢?第一种方式使用了一个Runnable对象,它更普遍,因为Runnable对象除了Thread还有其他的子类。第二种访法在简单的程序中使用会更简单,但是你的任务类必须继承Thread类,本课将会把注意力放在第一种方法上,它将线程对象执行的任务分离到了Runnable任务中。这种方式不仅更灵活,而且在后面涉及道德高级线程管理里也会有应用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类定义了很多对线程管理非常有用的方法。包括静态方法,它提供了一些消息,或调用这些方法会对线程的状态会有影响。在管理线程和Thread对象时,其他方法能够引起其他线程的调用。下面的章节我们将学习这些方法。