Java Thread Stop方法以及替换实现
2014-08-01 17:30
906 查看
/article/2833745.html
Stop方法不推荐使用,我给个具体的例子:
public class DeprecatedStop extends Object implements Runnable {
public void run() {
int count = 0;
while ( count <20 ) {
System.out.println("Running ... count=" + count);
count++;
try {
Thread.sleep(300);
} catch ( InterruptedException x ) {
// ignores
}
}
// the code maybe not executed
System.out.println("stoped");
}
public static void main(String[] args) {
DeprecatedStop ds = new DeprecatedStop();
Thread t = new Thread(ds);
t.start();
try {
Thread.sleep(2000);
} catch ( InterruptedException x ) {
// ignore
}
// Abruptly stop the other thread in its tracks!
t.stop();
}
}
可能的运行结果:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
可以发现程序中的打印stoped并没有执行,所以说如果在程序中有其他操作,如果线程突然stop是会带来严重的影响的。所以怎么也应该使用该操作。当然如果是我上面的程序代码突然stop的影响其实是没有的,但是如果是其他打开文件最后需要释放或者什么的就会带来严重的影响了。
如何在程序中对其进行停止呢?
public class AlternateStop extends Object implements Runnable {
private volatile boolean stopRequested;
private Thread runThread;
public void run() {
runThread = Thread.currentThread();
stopRequested = false;
int count = 0;
while ( !stopRequested ) {
System.out.println("Running ... count=" + count);
count++;
try {
Thread.sleep(300);
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt(); // re-assert interrupt
}
}
System.out.println("stoped");
}
public void stopRequest() {
stopRequested = true;
if ( runThread != null ) {
runThread.interrupt();
}
}
public static void main(String[] args) {
AlternateStop as = new AlternateStop();
Thread t = new Thread(as);
t.start();
try {
Thread.sleep(2000);
} catch ( InterruptedException x ) {
// ignore
}
as.stopRequest();
}
}
可能的运行结果如下:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
stoped
这样我们就解决了强制 stop的问题。
Stop方法不推荐使用,我给个具体的例子:
public class DeprecatedStop extends Object implements Runnable {
public void run() {
int count = 0;
while ( count <20 ) {
System.out.println("Running ... count=" + count);
count++;
try {
Thread.sleep(300);
} catch ( InterruptedException x ) {
// ignores
}
}
// the code maybe not executed
System.out.println("stoped");
}
public static void main(String[] args) {
DeprecatedStop ds = new DeprecatedStop();
Thread t = new Thread(ds);
t.start();
try {
Thread.sleep(2000);
} catch ( InterruptedException x ) {
// ignore
}
// Abruptly stop the other thread in its tracks!
t.stop();
}
}
可能的运行结果:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
可以发现程序中的打印stoped并没有执行,所以说如果在程序中有其他操作,如果线程突然stop是会带来严重的影响的。所以怎么也应该使用该操作。当然如果是我上面的程序代码突然stop的影响其实是没有的,但是如果是其他打开文件最后需要释放或者什么的就会带来严重的影响了。
如何在程序中对其进行停止呢?
public class AlternateStop extends Object implements Runnable {
private volatile boolean stopRequested;
private Thread runThread;
public void run() {
runThread = Thread.currentThread();
stopRequested = false;
int count = 0;
while ( !stopRequested ) {
System.out.println("Running ... count=" + count);
count++;
try {
Thread.sleep(300);
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt(); // re-assert interrupt
}
}
System.out.println("stoped");
}
public void stopRequest() {
stopRequested = true;
if ( runThread != null ) {
runThread.interrupt();
}
}
public static void main(String[] args) {
AlternateStop as = new AlternateStop();
Thread t = new Thread(as);
t.start();
try {
Thread.sleep(2000);
} catch ( InterruptedException x ) {
// ignore
}
as.stopRequest();
}
}
可能的运行结果如下:
Running ... count=0
Running ... count=1
Running ... count=2
Running ... count=3
Running ... count=4
Running ... count=5
Running ... count=6
stoped
这样我们就解决了强制 stop的问题。
相关文章推荐
- Java Thread Stop方法以及替换实现
- Java学习 - Thread的Stop方法以及替换实现
- Java Thread Stop方法以及替换实现
- Java Thread Stop方法以及替换实现
- java中俩种方法实现一个线程,以及不推荐使用stop()和suspend()方法
- Java 有几程方法可以实现一个线程?用什么关键字修饰同步?stop()和suspend()为什么不推荐使用?
- Java语言规范要求equals方法具有的特性以及实现方式
- java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用?
- java基础问题---java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用
- * java 中的数组 对象数组 以及main方法中的参数 x y不用中间参数实现交换
- java分别用Thread方法和Runable方法实现异步
- java创建多线程的两种方法(继承Thread类/实现Runnable接口)
- java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
- Java解决在浏览器地址栏中输入url访问action的问题以及拦截方法过滤的简易实现
- java 实现 linux+window mysql 文件 备份,以及文件导入错误解决方法
- 【转载】JAVA中线程的两种实现方法-实现Runnable接口和继承Thread类
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- Java反射高级应用--利用反射调用类的私有方法修改私有方法值,以及替换Java的类成员数据
- 完全二叉树的JAVA实现(以及非递归遍历方法)