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

【翻译三】java-并发之线程对象和实现的更多相关文章

  1. java并发之固定对象与实例

    java并发之固定对象与实例 Immutable Objects An object is considered immutable if its state cannot change after ...

  2. Java并发之线程间的协作

    上篇文章我们介绍了synchronized关键字,使用它可以有效的解决我们多线程所带来的一些常见问题.例如:竞态条件,内存可见性等.并且,我们也说明了该关键字主要是一个加锁和释放锁的集成,所有为能获得 ...

  3. Java并发之线程中断

    前面的几篇文章主要介绍了线程的一些最基本的概念,包括线程的间的冲突及其解决办法,以及线程间的协作机制.本篇主要来学习下Java中对线程中断机制的实现.在我们的程序中经常会有一些不达到目的不会退出的线程 ...

  4. java并发之线程同步(synchronized和锁机制)

    使用synchronized实现同步方法 使用非依赖属性实现同步 在同步块中使用条件(wait(),notify(),notifyAll()) 使用锁实现同步 使用读写锁实现同步数据访问 修改锁的公平 ...

  5. Java并发之线程管理(线程基础知识)

    因为书中涵盖的知识点比较全,所以就以书中的目录来学习和记录.当然,学习书中知识的时候自己的思考和实践是最重要的.说到线程,脑子里大概知道是个什么东西,但很多东西都还是懵懵懂懂,这是最可怕的.所以想着细 ...

  6. Java并发之线程转储

    一.java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个线程转储可能包含一个单独的线程或者多个线程.在多线程环境中,比如J2EE应用服务器,将会有许多线 ...

  7. Java并发之——线程池

    一. 线程池介绍 1.1 简介 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡 ...

  8. java并发之线程间通信协作

    在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...

  9. java并发之线程池的使用

    背景 当系统并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要消耗大量的系统资源. 所以需要一个办法使得线程可以 ...

随机推荐

  1. MVC框架 与Smarty

    MVC一种软件设计模式 MVC全名是 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据. ...

  2. (原)android中的动画(二)

    帧动画的使用需要在xml文件中指定每一帧所对应的图片 animation-list写法如下: <?xml version="1.0" encoding="utf-8 ...

  3. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  4. django xadmin 外键

    style_fields = {'db栏位名称': "fk-ajax"} 实体关系: Account (*)-->(1) user 表单控件: 下拉框 美化用了selecti ...

  5. java面试宝典(蓝桥学院)

    Java面试宝典(蓝桥学院) 回答技巧 这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的学生在笔试/面试时更好地赢得好的结果.由于这套试题涉及的范围 ...

  6. UNIX进程

    UNIX进程控制的博客   http://blog.csdn.net/yang_yulei/article/details/17404021 Linux的概念与体系    http://www.cnb ...

  7. C++实现VPN工具之VPN错误代码大全

    该篇文章转自:<VPN问题全攻略>http://home.51.com/h012359/diary/item/10008457.html 以下是使用VPN版软件中常见的一些错误代码: 1. ...

  8. Java集合中Comparator和Comparable接口的使用

    在Java集合中,如果要比较引用类型泛型的List,我们使用Comparator和Comparable两个接口. Comparable接口 -- 默认比较规则,可比较的 实现该接口表示:这个类的实例可 ...

  9. selenium使用actions.moveToElement处理菜单

    //should set firefox path //FirefoxBinary binary=new FirefoxBinary(new File("C:\\Program Files ...

  10. UVALive 6577 Binary Tree 二叉树的LRU串

    今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一 ...