博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Defining and Starting a Thread
阅读量:5070 次
发布时间:2019-06-12

本文共 3295 字,大约阅读时间需要 10 分钟。

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.

这节主要记载了线程对象的使用。执行者会与其他高级的同步对象一起讨论

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:
应用程序创建一个线程的实例必须提供将要在这个线程中运行的代码。有2种方法实现这个。
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,这个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:
继承Thread。Thread类实现了Runnable接口,尽管它的run方法什么也没干。应用程序可以继承Thread类,提供他自己的run方法实现。就像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.
主要上面2个例子都是调用Thread.start类启动一个新线程
Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable 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.
你将会使用哪种方式呢?第一种方式使用了一个Runnable对象,它更普遍,因为Runnable对象除了Thread还有其他的子类。第二种访法在简单的程序中使用会更简单,但是你的任务类必须继承Thread类,本课将会把注意力放在第一种方法上,它将线程对象执行的任务分离到了Runnable任务中。这种方式不仅更灵活,而且在后面涉及道德高级线程管理里也会有应用
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类定义了很多对线程管理非常有用的方法。包括静态方法,它提供了一些消息,或调用这些方法会对线程的状态会有影响。在管理线程和Thread对象时,其他方法能够引起其他线程的调用。下面的章节我们将学习这些方法。

转载于:https://www.cnblogs.com/yuwenxing/archive/2012/05/16/2505393.html

你可能感兴趣的文章
fastJson java后台转换json格式数据
查看>>
php实现定时计划任务
查看>>
架构升级中并发容器的使用的一些方法
查看>>
windows下如何批量修改文件名
查看>>
不同版本系统执行不同注册表的脚本
查看>>
计算智能 Computational Intelligence,CI
查看>>
EXCEL表导入SQL,出现错误 0xc02020c5 的问题解决
查看>>
【六】jquery之HTML代码/文本/值[下拉列表框、多选框、单选框的选中]
查看>>
Spring框架学习笔记(5)——自动装配
查看>>
JS—一维数组的创建
查看>>
零基础入门Python3-函数式编程(1)
查看>>
SVN服务器的搭建与使用(详细图解)
查看>>
Windows程序设计笔记(二) 关于编写简单窗口程序中的几点疑惑
查看>>
补充第一次考试
查看>>
【转】Rerouting requests to a UCMA application with MSPL
查看>>
oracle中创建sequence指定起始值
查看>>
java 设计模式学习笔记九 decorator装饰模式
查看>>
ZOJ2760_How Many Shortest Path
查看>>
2016 CCPC 合肥赛区 平行四边形//打铁记录..... 背锅还是我在行 此处@ctr 233
查看>>
max
查看>>