Topic: Thread 中的stop()怎么用?

  Print this page

1.Thread 中的stop()怎么用? Copy to clipboard
Posted by: luoyh
Posted on: 2005-04-06 23:15

Thread 中的stop()怎么用?

怎样可以使线程立即结束?

2.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: cxp108
Posted on: 2005-04-13 18:06

直接调用stop()是可以让线程立即结束,但在垃圾收集上有诸多隐患。

最好的线程结束方式就是从 run()方法中返回。如果真的迫不得以用stop()

方法,那最好覆写 finalize()方法保证所有的资源能被正确回收。

3.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: floater
Posted on: 2005-04-13 23:18

don't use stop(), ever.

4.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: 九佰
Posted on: 2005-05-10 19:05

线程中的suspend()和stop() 方法不安全。

可以在run中处理,如下:

public class ThreadTest{

public static void main(String args[]){

// 创建一个线程,启动

MyThread thread = new MyThread();
thread.start();
// Do work...

// 停止
thread.allDone = true;
}

class MyThread extends Thread {
boolean allDone = false;

public void run() {
while (true) {
// Do work...
if (allDone) {
return;
}
// Do work...
}
}
}

5.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: ftang
Posted on: 2005-05-12 00:13

Any one should not use stop() ever....This method is deprecated by Sun, and here is the reason...

http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

6.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-05-12 15:50

it is Deprecated.

7.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: ftang
Posted on: 2005-05-12 22:37

Deprecated is not Forbidden, but you are on your own risk. the only one I met using Deprecated method without any doubt is a programmer from SUN, which is a system developer working for sparc.
normal application developer should always to avoid use deprecated method to improve the stablility of software.

8.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: henryevol
Posted on: 2005-06-04 17:59

中止一个线程的方法
1 while(isRunning){} 置 isrunning为false
2 如果有IO操作,关闭IO,让IO抛出异常中止 防止 io阻塞线程
3 如果循环中有 sleep 或 wait 方法的调用 调用 interrupt 方法中止由 sleep或 wait方法产生的阻塞

9.Re:Thread 中的stop()怎么用? [Re: luoyh] Copy to clipboard
Posted by: yinpeng263
Posted on: 2005-06-08 04:10

http://www.java2s.com/ExampleCode/Threads/Simple-Threads.htm


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923