线程对象

(本部分原文连接译文连接,译者:郑旭东) html

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

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: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 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:
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对象。咱们将在下面的章节中,深刻探讨这些内容。 

相关文章
相关标签/搜索