The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
An application that creates an instance of 创建Thread
must provide the code that will run in that thread. Thread
实例的应用程序必须提供将在该线程中运行的代码。There are two ways to do this:有两种方法可以做到这一点:
Runnable
object.Runnable
的对象。 Runnable
interface defines a single method, run
, meant to contain the code executed in the thread. Runnable
接口定义了一个方法run
,用于包含线程中执行的代码。Runnable
object is passed to the Thread
constructor, as in the HelloRunnable
example: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(); } }
Thread
. Thread
。Thread
class itself implements Runnable
, though its run
method does nothing. Thread
类本身实现了Runnable
,尽管它的run
方法什么都不做。Thread
, providing its own implementation of run
, as in the HelloThread
example:Thread
进行子类化,提供自己的运行实现,如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.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
. Runnable
对象,它更为一般,因Runnable
对象可以为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
. Thread
的后代这一事实的限制。This lesson focuses on the first approach, which separates the 本课程重点介绍第一种方法,它将Runnable
task from the Thread
object that executes the task. Runnable
任务与执行任务的Thread
对象分离。Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.这种方法不仅更加灵活,而且适用于后面介绍的高级线程管理API。
The Thread
class defines a number of methods useful for thread management. Thread
类定义了许多对线程管理有用的方法。These include 其中包括static
methods, which provide information about, or affect the status of, the thread invoking the method. static
方法,这些方法提供有关调用该方法的线程的信息或影响该线程的状态。The other methods are invoked from other threads involved in managing the thread and 其他方法从管理线程和Thread
object. Thread
对象所涉及的其他线程调用。Well examine some of these methods in the following sections.我们将在下面的部分中研究其中的一些方法。