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.

译文:
线程对象
每一个线程都和Thead类的一个实例有关系。这里对Thread对象创建并发程序有两个基本的分类。
  • 为了直接控制和管理线程,可以在应用程序每次需要异步执行任务的时候简单的实现Threa对象即可。
  • 为了从应用程序的其他地方抽象的管理线程,可以传递应用程序的任务到executor。

这个课程描述了Thread对象的使用。关于executors的讨论放在了其他high-level concurrency 对象种。

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:

  • 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:

    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的实例必须提供能在线程中运行的代码。这里有两种方法做这件事情:

  • 提供一个Runnable对象,这个Runnable接口定义了单个方法,run方法,包含能够在线程中执行的代码。正如在HelloRunnable的实例中一样,这个Runnable对象通过Thread的构造方法传递。
 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类通过一个不包括任何东西的run方法实现了Runnable.如同HelloThread实例,一个应用程序可以继承自Thread,通过重写run方法来实现多线程。
 public class HelloThread extends Thread {

     public void run() {
System.out.println("Hello from a thread!");
} public static void main(String args[]) {
(new HelloThread()).start();
}
}

注意:两种方法都调用了start方法来启动这个线程。

这里是否有什么约定俗称的规则了?第一个规则,用runnable对象的方法,更加的通用。因为runnable对象不仅能实现thread类,而且能实现一个子类。第二个是更加容易在简单的程序中使用,但是,有一个事实就是你的任务类必须是Thread类的子类。这个课程主要集中于第一中实现方法,这个方法使得runnable对象的实现和thread对象的执行分开。不仅仅是因为这种方法更加的灵活,也因为它对一个讨论的高性能的并发也是兼容的。

Thread类定义了大量对线程管理有用的方法。其中包括静态方法,它提供的信息或者影响的状态,线程执行这个方法。被其他的线程执行管理线程或者线程对象的涉及到其他的方法。我们将会在接下来的课程中介绍这些方法。

最新文章

  1. C#和Java中的Substring()
  2. spring ioc 源码解析
  3. spring ioc三种注入方式
  4. How do you build a database?
  5. 第三章 数组与字符串 UVa1588 Kickdown
  6. Linux Apache和Nginx的比较
  7. 转载:XPath基本语法
  8. Html11.09CSS层叠样式表内容整理
  9. 怎么SDCard上的获取相册照片
  10. PL/SQL数据导入导出浅谈(1)
  11. 如何监控 Tomcat?Zabbix 与 Cloud Insight 对比
  12. core java 第四章笔记
  13. Scrambled Polygon(斜率排序)
  14. Python字符串和日期相互转换的方法
  15. Java面向对象——类,对象和方法
  16. 工控随笔_17_西门子_WinCC的VBS脚本_06_过程和函数
  17. Elasticsearch实践(二):搜索
  18. Floyd算法_MATLAB
  19. Android开发属性动画
  20. (18)jq事件操作

热门文章

  1. ACM - a + b Problem
  2. python 字节与字符串转换
  3. 子类重载父类的方法“parent::方法名”转于 恩聪PHP学习教程
  4. 细化如何安装LNMP + Zabbix 监控安装文档以及故障排除
  5. Maven打包附加配置文件
  6. ios中json解析出现的null问题
  7. WCF 定制自己的签名验证逻辑
  8. Appium 客户端库 API
  9. CentOS 7部署flume
  10. Java多线程Socket在控制台输出的多人聊天室编程