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.java
To directly control thread creation and management, simply instantiate Thread
each time the application needs to initiate an asynchronous task.api
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.oracle
在Java中,每一個線程都是Thread類的實例。併發應用中通常有兩種不一樣的線程建立策略。
app
直接控制線程的建立和管理,每當應用程序須要執行一個異步任務的時候就爲其建立一個線程。less
將線程的管理從應用程序中抽象出來做爲執行器,應用程序將任務傳遞給執行器,有執行器負責執行。異步
這一節,咱們將討論Thread對象,有關Executors將在高級併發對象一節中討論。 async
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:ide
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
example:HelloRunnable
定義並啓動一個線程
應用程序在建立一個線程實例時,必須提供須要在線程中運行的代碼。有兩種方式去作到這一點:
提供一個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(); } }
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:
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.start()方法來啓動一個新的線程。 哪種方式是咱們應該使用的?相對來講,第一種更加通用,由於Runnable對象能夠繼承於其餘類(Java只支持單繼承,當一個類繼承於Thread類後,就沒法繼承於其餘類)。第二種方法更易於在簡單的應用程序中使用,但它的侷限就是:你的任務類必須是Thread的子類。這個課程更加聚焦於第一種將Runnable任務和Thread類分離的方式。不單單是由於這種方式更加靈活,更由於它更適合後面將要介紹的高級線程管理API。 Thread類定義了一些對線程管理十分有用的的方法。在這些方法中,有一些靜態方法能夠給當前線程調用,它們能夠提供一些有關線程的信息,或者影響線程的狀態。而其餘一些方法能夠由其餘線程進行調用,用於管理線程和Thread對象。咱們將在下面的章節中,深刻探討這些內容。