The Java™ Tutorials — Concurrency :Defining and Starting a Thread 线程的定义和启动

The Java™ Tutorials — Concurrency :Defining and Starting a Thread 线程的定义和启动

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

关键点

  • 创建Thread的两种方式: 
    • 利用Runnable 
      • 样例:new Thread(new HelloRunnable())
    • 继承Thread 
      • 样例:class MyThread extends Thread {...}
  • 两种方式的选择: 
    • 第一种方式较为灵活常见,因为Runnable对象可以通过任何一个类实现,而不仅仅是Thread。 第二种则需要保证为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. 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()方法,这个方法负责装载于线程中执行的代码。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.

注意,两个案例都调用了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任务从将要执行它的线程中分离出来。这种方法不仅仅更加易用,而且它更适用之后所写的高版本线程管理API。

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对象管理等功能。我们会在下面的章节里检验其中的部分方法。

你可能感兴趣的:(thread,Runnable,并发,线程,Concurrent)