您的位置:首页 > 编程语言 > Java开发

Java Thread Stop方法以及替换实现

2016-07-14 16:08 417 查看
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 - 实习生 一级
最佳答案

检举


stop@Deprecatedpublic final void stop() 已过时。 该方法具有固有的不安全性。用 Thread.stop 来终止线程将释放它已经锁定的所有监视器(作为沿堆栈向上传播的未检查 ThreadDeath 异常的一个自然后果)。如果以前受这些监视器保护的任何对象都处于一种不一致的状态,则损坏的对象将对其他线程可见,这有可能导致任意的行为。stop 的许多使用都应由只修改某些变量以指示目标线程应该停止运行的代码来取代。目标线程应定期检查该变量,并且如果该变量指示它要停止运行,则从其运行方法依次返回。如果目标线程等待很长时间(例如基于一个条件变量),则应使用
interrupt 方法来中断该等待。有关更多信息,请参阅《为何不赞成使用 Thread.stop、Thread.suspend 和 Thread.resume?》。 强迫线程停止执行。 如果安装了安全管理器,则以 this 作为其参数调用 checkAccess 方法。这可能引发 SecurityException(在当前线程中)。 如果该线程不同于当前线程(即当前线程试图终止除它本身以外的某一线程),则安全管理器的 checkPermission 方法(带有 RuntimePermission("stopThread")
参数)也会被调用。这会再次抛出 SecurityException(在当前线程中)。 无论该线程在做些什么,它所代表的线程都被迫异常停止,并抛出一个新创建的 ThreadDeath 对象,作为异常。 停止一个尚未启动的线程是允许的。如果最后启动了该线程,它会立即终止。 应用程序通常不应试图捕获 ThreadDeath,除非它必须执行某些异常的清除操作(注意,抛出 ThreadDeath 将导致 try 语句的 finally 子句在线程正式终止前执行)。如果 catch 子句捕获了一个 ThreadDeath
对象,则重新抛出该对象很重要,因为这样该线程才会真正终止。 对其他未捕获的异常作出反应的顶级错误处理程序不会打印输出消息,或者另外通知应用程序未捕获到的异常是否为 ThreadDeath 的一个实例。 抛出: SecurityException - 如果当前线程不能修改该线程。 另请参见: interrupt(), checkAccess(), run(), start(), ThreadDeath, ThreadGroup.uncaughtException(java.lang.Thread, java.lang.Throwable),
SecurityManager.checkAccess(Thread), SecurityManager.checkPermission(java.security.Permission) 1

回答者: aegeaner - 实习生 一级

2008-6-24
12:22
我来评论>>提问者对于答案的评价:十分感谢

收藏于 2009-11-24
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程 对象 java