【翻译三】java-并发之线程对象和实现
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.
- 为了直接控制和管理线程,可以在应用程序每次需要异步执行任务的时候简单的实现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. TheRunnable
interface defines a single method,run
, meant to contain the code executed in the thread. TheRunnable
object is passed to theThread
constructor, as in the
example: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
. TheThread
class itself implementsRunnable
, though itsrun
method does nothing. An application can subclassThread
, providing its own implementation ofrun
, as in the
example: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.
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-并发之线程对象和实现的更多相关文章
- java并发之固定对象与实例
java并发之固定对象与实例 Immutable Objects An object is considered immutable if its state cannot change after ...
- Java并发之线程间的协作
上篇文章我们介绍了synchronized关键字,使用它可以有效的解决我们多线程所带来的一些常见问题.例如:竞态条件,内存可见性等.并且,我们也说明了该关键字主要是一个加锁和释放锁的集成,所有为能获得 ...
- Java并发之线程中断
前面的几篇文章主要介绍了线程的一些最基本的概念,包括线程的间的冲突及其解决办法,以及线程间的协作机制.本篇主要来学习下Java中对线程中断机制的实现.在我们的程序中经常会有一些不达到目的不会退出的线程 ...
- java并发之线程同步(synchronized和锁机制)
使用synchronized实现同步方法 使用非依赖属性实现同步 在同步块中使用条件(wait(),notify(),notifyAll()) 使用锁实现同步 使用读写锁实现同步数据访问 修改锁的公平 ...
- Java并发之线程管理(线程基础知识)
因为书中涵盖的知识点比较全,所以就以书中的目录来学习和记录.当然,学习书中知识的时候自己的思考和实践是最重要的.说到线程,脑子里大概知道是个什么东西,但很多东西都还是懵懵懂懂,这是最可怕的.所以想着细 ...
- Java并发之线程转储
一.java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个线程转储可能包含一个单独的线程或者多个线程.在多线程环境中,比如J2EE应用服务器,将会有许多线 ...
- Java并发之——线程池
一. 线程池介绍 1.1 简介 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡 ...
- java并发之线程间通信协作
在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...
- java并发之线程池的使用
背景 当系统并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要消耗大量的系统资源. 所以需要一个办法使得线程可以 ...
随机推荐
- git 教程(5)--工作区和暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 工作区(working directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库 ( ...
- c# winform UI + python底层的一点尝试
鉴于python做winform之类的UI比较弱.于是想结合C#的winform 和 python的底层开发(windows平台). 尝试做了一个RSS阅读器.在这里:http://download. ...
- [转]C++中四种类型转换符的总结
C++中四种类型转换符的总结 一.reinterpret_cast用法:reinpreter_cast<type-id> (expression) reinterpret_cast操 ...
- poj 3268(spfa)
http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...
- 压测 apache ab 初探
2015年10月30日 14:58:34 ab是apache自带的压测命令, 在其bin目录下边, 不仅可以压测Apache, 也可以测nginx或其他服务器 可以模拟上传post值 (-p, 与下边 ...
- QListWidget
1.失去焦点背景颜色,代码设置全选的时候,背景会是白色,需要设置失去焦点背景颜色.(设置焦点,会出现白转化成设置背景色,效果不好) QPalette p; p.setColor(QPalette::I ...
- js数组转置
<script type="text/javascript"> var arr=[[1,2,3],[4,5,6],[7,8,9],[17,18,19]]; ...
- 请确认 <Import> 声明中的路径正确,且磁盘上存在该文件。
在网上下了个源码打开报错. 请确认 <Import> 声明中的路径正确,且磁盘上存在该文件. 一查,原来是路径错误. 解决办法:将项目文件(.csproj)用记事本打开,然后找到<I ...
- 【python】入门学习(十)
#入门学习系列的内容均是在学习<Python编程入门(第3版)>时的学习笔记 统计一个文本文档的信息,并输出出现频率最高的10个单词 #text.py #保留的字符 keep = {'a' ...
- MAC系统下,删除.svn文件
MAC系统下,.svn文件是隐藏的. 如果项目是非export导出的,那么项目中会有很多的.svn文件. 如果项目的体积非常庞大,我们如何快速的批量删除.svn文件呢?下面是操作方法: 打开终端,cd ...