它已停止运行后,线程不会被删除 [英] Thread isn't removed after it has stopped running
问题描述
我有是在一个线程中运行的服务。当我需要线程停止运行我用这code结果 this.serviceThread.interrupt();
this.serviceThread = NULL;
在某些时候,我需要再次重新创建线程
this.serviceThread =新主题()
{
公共无效的run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
因为它是在当前正在运行的线程的列表中列出
然而,它仍然似乎是previous线程仍然活着并运行。这份名单只是不断每次我试图阻止,并创建一个新的线程时间增长。这正常吗?反正我有可以摆脱那些老线程?
我主要就是想知道,如果线程该名单意味着他们仍然存在,如果是这样,我怎么能删除它们。谢谢!
编辑:这是怎么了处理运行/停止线程
公共无效startProcessThread()
{
this.shutdown = FALSE;
this.serviceThread =新主题()
{
公共无效的run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
}
私人无效serviceProcessThread()
{
做
{
尝试
{
this.getCommands(); 如果(this.tasks.size()大于0)
this.processTasks(); 如果(!this.shutdown)
{
视频下载(ServiceSleepTime);
}
}
赶上(例外五)
{
this.logException(serviceProcessThread,E);
}
}
而(!this.shutdown);
如果(this.serviceThread!= NULL)
{
this.serviceThread.interrupt();
this.serviceThread = NULL; }}
你必须面对的第一件事是,一个线程不能被强制停止,而不会对整个Java进程的潜在不利影响。这就是为什么Java的线程推出的机制的中断的:一个共同的,的合作的机制,以正常停止线程
的合作的方面是关键:无论你在你的线程的执行code做,你必须确保它是中断的。一个简短的清单:
-
如果您已阻塞调用(那些逮捕线程,同时在条件等),他们必须中断(基本上,声明抛出
InterruptedException的
); -
您必须捕获并处理
InterruptedException的
正常,应通过执行任何因清理和制作顶级运行
方法的返回; -
如果你已经实现了长期运行的循环,你必须确保定期检查
Thread.currentThread()。isInterrupted()
标志和休息如果线程被中断; -
如果你放弃控制任何第三方code,确保该code是中断的。
另外请记住,主题的生命周期
作为一个Java对象无关的执行的实际线程。 发
仅仅是一个处理对象,它可以让您管理底层线程,就像一个文件
是一个文件系统中的手柄实体。设置文件
参照空
不从系统中删除该文件。
更新
您执行code可以通过吞咽不固定的 InterruptedException的
。
{尝试
this.getCommands();
如果(this.tasks.size()大于0)
this.processTasks();
如果(!this.shutdown)
视频下载(ServiceSleepTime);
}
赶上(InterruptedException的E){
this.shutdown = TRUE;
}
赶上(例外五)
{
this.logException(serviceProcessThread,E);
}
此外,这部分是多余的
如果(this.serviceThread!= NULL)
{
this.serviceThread.interrupt();
this.serviceThread = NULL;}
在这里,你试图打断你自己(当前)线程。问题的关键是,线程应的其他的线程中断。
I have a Service which is running on a thread. When I need the thread to stop running I am using this code
this.serviceThread.interrupt();
this.serviceThread = null;
At some point I need to recreate the thread again
this.serviceThread = new Thread()
{
public void run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
However, it still seems like the previous Thread is still alive and running because it is listed in the list of currently running threads. This list just keeps growing every time I try to stop and create a new thread. Is this normal? Is there anyway I can get rid of those old threads?
I mainly just want to know if that list of threads means they are still there and, if so, how can I remove them. Thanks!
EDIT: This is how I am handling running/stopping the thread
public void startProcessThread()
{
this.shutdown = false;
this.serviceThread = new Thread()
{
public void run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
}
private void serviceProcessThread()
{
do
{
try
{
this.getCommands();
if (this.tasks.size() > 0)
this.processTasks();
if (!this.shutdown)
{
Thread.sleep(ServiceSleepTime);
}
}
catch (Exception e)
{
this.logException("serviceProcessThread", e);
}
}
while (!this.shutdown);
if(this.serviceThread != null)
{
this.serviceThread.interrupt();
this.serviceThread = null;
}
}
The first thing you must face is that a thread cannot be forcibly stopped without potentially adverse effects on the whole Java process. This is why Java introduced the mechanism of thread interruption: a common, cooperative mechanism to gracefully stop a thread.
The cooperative aspect is key: whatever you do in your thread's implementation code, you must ensure that it is interruptible. A short checklist:
if you have blocking calls (those which arrest the thread while waiting on a condition), they must be interruptible (basically, declare to throw
InterruptedException
);you must catch and handle the
InterruptedException
properly, by performing any due cleanup and making the top-levelrun
method return;if you have implemented a long-running loop, you must ensure that it checks the
Thread.currentThread().isInterrupted()
flag periodically, and breaks if the thread was interrupted;if you cede control any 3rd party code, make sure that this code is interruptible.
Also keep in mind that the lifecycle of Thread
as a Java object has nothing to do with the actual thread of execution. Thread
is merely a handle object which lets you manage the underlying thread, much as a File
is a handle for a filesystem entity. Setting a File
reference to null
does not delete the file from the system.
Update
Your implementation code could be fixed by not swallowing the InterruptedException
.
try {
this.getCommands();
if (this.tasks.size() > 0)
this.processTasks();
if (!this.shutdown)
Thread.sleep(ServiceSleepTime);
}
catch (InterruptedException e) {
this.shutdown = true;
}
catch (Exception e)
{
this.logException("serviceProcessThread", e);
}
Also, this part is redundant:
if(this.serviceThread != null)
{
this.serviceThread.interrupt();
this.serviceThread = null;
}
Here you attempt to interrupt your own (current) thread. The point was that the thread should be interrupted from another thread.
这篇关于它已停止运行后,线程不会被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!