Stopping Thread in Java

2020-04-17 03:56发布

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条回答
倾城 Initia
2楼-- · 2020-04-17 04:06

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.

查看更多
登录 后发表回答