Java Thread Stop方法以及替换实现
来源:互联网 发布:melanmeka淘宝网 编辑:程序博客网 时间:2024/06/05 15:38
Java Thread 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的问题。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/longronglin/archive/2008/11/29/3408510.aspx
java中resume() stop()方法已经过时用什么方法代替呢 悬赏分:0 - 解决时间:2008-9-28 09:23 提问者: yftk89 - 实习生 一级 最佳答案- 检举
回答者: aegeaner - 实习生 一级 2008-6-24 12:22
我来评论>>提问者对于答案的评价:十分感谢- Java Thread Stop方法以及替换实现
- Java Thread Stop方法以及替换实现
- Java Thread Stop方法以及替换实现
- Java Thread Stop方法以及替换实现
- Java学习 - Thread的Stop方法以及替换实现
- Java中的线程Thread方法之---stop()
- java stop thread
- java中俩种方法实现一个线程,以及不推荐使用stop()和suspend()方法
- Thread中的stop方法过时
- 为什么Thread.stop()方法被弃用
- java如何正确停止Thread,thread.stop ?
- 为什么不能使用Thread.stop()方法?
- Thread的Suspend和Stop方法
- 为什么不能使用Thread.stop()方法?
- Thread.stop()
- Java - Start and Stop a Thread
- 《综》Java Thread - stop 问题 and 解决方法
- java thread的stop,suspend,resume等方法废弃的原因
- mysql主从设置
- [转]SVN中的Branches分支以及Tags标签详解与应用举例
- 教你写一个简单的网页(html网页开发入门)
- Android热修复
- linux命令login
- Java Thread Stop方法以及替换实现
- 深入分析C++引用
- jQuery事件绑定on()、bind()与delegate() 方法详解
- hdu 1088 Write a simple HTML Browser
- HDU 3605 网络流
- 在引入layout文件的时候出现resourse not found的解决问题
- 再谈闭包-词法作用域
- npm执行无响应
- 委托的学习总结(续)