class MyThread extends Thread {
private boolean stopRunning = false; // <-- 标志
public void stopRunning() { stopRunning=true; } public void run() {
while(!stopRunning) {
// 线程代码放此
}
}}这样就可以安全的结束线程:
MyThread t = new MyThread();
t.start();
...
...
t.stopRunning();
private boolean stopRunning = false; // <-- 标志
public void stopRunning() { stopRunning=true; } public void run() {
while(!stopRunning) {
// 线程代码放此
}
}}这样就可以安全的结束线程:
MyThread t = new MyThread();
t.start();
...
...
t.stopRunning();
解决方案 »
- 通过myeclipse中发布到tomcat中运行没有问题,达成war包部署就出错
- 这种情况如何写spring
- 很简单字符串相加的问题
- spring中如果dao为非单例,singleton="false";发现bean不能被回收导致内存溢出,怎么解决
- 急!struts 上传文件 的时候,过滤器报内存溢出
- 怎样编写DTD来控制XML文件中的元素
- apache2.0.47+tomcat5.+JK2 整合问题!!!
- -====我是加加,十分迷恋编程,有个问题请教大家,谢谢:) ===-
- DATASOURCE问题
- 不知道国内的J2EE程序员多不多?
- 补充刚才的,高分求助!!!!!
- 在使用eclipse建立j2ee项目时的问题?请各位指点迷津?
Deprecated. 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 ThreadDeath 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 stop 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 interrupt method should be used to interrupt the wait. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
void stop(Throwable obj)
Deprecated. This method is inherently unsafe. See stop() (with no arguments) for details. An additional danger of this method is that it may be used to generate exceptions that the target thread is unprepared to handle (including checked exceptions that the thread could not possibly throw, were it not for this method). For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
void suspend()
Deprecated. This method has been deprecated, as it is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?. 手动结束线程的事情还是不要做了