为什么Java不支持重启线程 [英] Why java doesn't support restarting a thread

查看:71
本文介绍了为什么Java不支持重启线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何在Java中启动/停止/重新启动线程?

Possible Duplicate:
How to start/stop/restart a thread in Java?

1.如果在正在运行的线程上调用start(),这是一个错误 2.如果在停止的线程上调用start(),则不会发生任何事情.

1.If you call start() on a running thread it's an error 2.If you call start() on a stopped thread nothing happens.

不支持在同一对象上重新启动的背后原因是什么?

What's the reasoning behind not supporting restarts over the same object?

推荐答案

我认为设计师之所以这么做,是因为OS级线程通常以这种方式运行-创建,运行,然后销毁它们以及操作系统执行清理.因此,Java设计人员可能希望Java中的线程概念与大多数操作系统中的线程接近.方法start()stop()并不是要暂停线程(为此我们进行了同步),只是为了让它运行并销毁对象.也许名字有​​点混乱.请注意,stop()已过时,不应使用,因此,如果我们取消stop(),则名称start()不会再造成混乱.

I think the designers did it because OS-level threads usually behave in this way - they are created, they may run, then they are destroyed and the operating system performs cleanup. So probably Java designers wanted the thread notion in Java to be close to what a thread is in most operating systems. The methods start() and stop() are not meant to pause the thread (we have synchronization for that), only for letting it run and destroying the object. Maybe the names are a bit confusing. Note that stop() is deprecated and should not be used, so if we eliminate stop(), the name start() is not as confusing any more.

这篇关于为什么Java不支持重启线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆