- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java终止线程实例和stop()方法源码阅读由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
了解线程 。
概念 。
线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程.
线程特点 。
拥有状态,表示线程的状态,同一时刻中,JVM中的某个线程只有一种状态,
·NEW 。
尚未启动的线程(程序运行开始至今一次未启动的线程) 。
·RUNNABLE 。
可运行的线程,正在JVM中运行,但它可能在等待其他资源,如CPU.
·BLOCKED 。
阻塞的线程,等待某个锁允许它继续运行 。
·WAITING 。
无限等待(再次运行依赖于让它进入该状态的线程执行某个特定操作) 。
·TIMED_WAITING 。
定时等待(再次运行依赖于让它进入该状态的线程在指定等待时间内某个特定操作) 。
·TERMINATED 。
已退出的线程 。
拥有优先级,决定线程的执行顺序,
1至10之间的整数,默认数值为5。数值越高,执行的几率越高,优先级并不能决定线程的执行顺序.
子线程的优先级默认同父线程的一样.
注意,当以下情况发生时,JVM将停止执行所有线程:
Runtime(运行时)的exit ()方法被调用并且该方法的调用被Security Manager所允许,
所有的“非守护线程”都已停止运行(无论时正常停止还是一场停止),
可以被标记为守护程序(Daemon) 。
守护线程的子线程仍是守护线程,
守护线程也就是“后台线程”,一般用来执行后台任务,而用户线程一般用户执行用户级任务.
终止线程的方法 。
1.使用退出标志,使线程正常退出,也就是当run方法完成后线程终止.
当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子.
FlagExitThread.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
com.rainmonth;
/**
* Created by RandyZhang on 2017/3/23.
*/
public
class
FlagExitThread
extends
Thread {
public
volatile
Boolean isExit =
false
;
public
FlagExitThread(String name) {
super
(name);
}
@Override
public
void
run() {
while
(!isExit) {
System.out.println(
"I'm running"
);
}
}
}
|
DemoClient.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package
com.rainmonth;
/**
* Created by RandyZhang on 2017/3/23.
*/
public
class
DemoClient {
public
static
void
main(String[] args) {
System.out.println(
"优雅的终止线程实例"
);
exitByFlag();
// exitByInterrupt();
}
private
static
void
exitByFlag() {
FlagExitThread flagExitThread =
new
FlagExitThread(FlagExitThread.
class
.getSimpleName());
flagExitThread.start();
try
{
Thread.sleep(
1000
);
flagExitThread.isExit =
true
;
flagExitThread.join();
System.out.println(
"线程退出"
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
private
static
void
exitByInterrupt() {
FlagExitThread flagExitThread =
new
FlagExitThread(FlagExitThread.
class
.getSimpleName());
System.out.println(
"flagExitThread running..."
);
flagExitThread.start();
try
{
Thread.sleep(
1500
);
System.out.println(
"flagExitThread interrupted..."
);
flagExitThread.interrupt();
Thread.sleep(
1500
);
System.out.println(
"stop application..."
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
|
输出结果:
打印了一大堆I'm running之后线程退出.
2.使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果).
显示调用stop()方法。源码中关于stop() 的描述如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/*
* This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
* <code>ThreadDeath</code> exception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
* other threads, potentially resulting in arbitrary behavior. Many
* uses of <code>stop</code> should be replaced by code that simply
* modifies some variable to indicate that the target thread should
* stop running. The target thread should check this variable
* regularly, and return from its run method in an orderly fashion
* if the variable indicates that it is to stop running. If the
* target thread waits for long periods (on a condition variable,
* for example), the <code>interrupt</code> method should be used to
* interrupt the wait.
*/
|
大意就是说,该方法的不安全性时固有的。调用stop()终止一个线程会释放它已经锁定的所有监视器(这将导致沿堆栈向上传播为检查的ThreadDeath异常被抛出),若此时之前受这些被释放的监视器保护的对象存在不一致性,并且这些对象对其他线程可见,这就会导致一些意想不到的后果。stop操作应该有哪些仅仅只需要修改某些代码就可以指示目标线程应该停止运行的代码来取代(方法一就是这种方式)。如果目标线程由于等待某一条件(如某个条件变量)等待很长时间,我们应该使用interrupt方法来中断该等待(方法三就是这种方式).
3.使用interrupt方法中断线程.
interrupt字面上是终止的意思,但不要试图通过调用interrupt来终止线程,因为有时即使你调用了该方法,线程仍然会继续执行,可以注释掉上面的exitByFlag(),开启exitByInterrupt() 方法,发现及时调用了interrupt()方法,仍在一直输出I'm running…(不同系统及CPU结果可能有所不同),可见采用interrupt方式也是不安全的.
总结 。
根据以上的分析,最值得推荐的方式是第一种,我们可以用共享变量(shared variable)的方式来设置标志,并发出信号,通知线程必须终止。当然对这个共享变量的操作我们必须保证是同步的.
以上就是本文关于Java终止线程实例和stop()方法源码阅读的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! 。
原文链接:http://blog.csdn.net/rainmonth/article/details/65445467 。
最后此篇关于Java终止线程实例和stop()方法源码阅读的文章就讲到这里了,如果你想了解更多关于Java终止线程实例和stop()方法源码阅读的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!