Stopping Thread in Java

僤鯓⒐⒋嵵緔 提交于 2019-12-20 03:30:33

问题


if i have got such java code:

public static void main(String[] args)
{
    for(int i = 0;i<100;i++)
    {
        Future<?> f = ThreadPoolManager.getInstance().schedule(new START(), 500);
        f.cancel(true);
    }
}
private class START implements Runnable
{
    @Override
    public void run()
    {
        System.out.println(1);
    }
}

And run it in debug, i can see that all of those threads(after cancel) are still running, so are they taking my memory too? And if yes, how can i destroy those Threads completely?


回答1:


cancel(true) calls interrupt() on your thread, nothing more. So, you need to handle it properly in your run() method. For your simple case your threads will finish their execution and their objects will be cleared by GC.



来源:https://stackoverflow.com/questions/12330013/stopping-thread-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!