This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 看这里的DX好像对Java都很有研究的样子,决定出个小问题考一下,想必各位DX对我这么幼稚的问题会进行一番嘲笑。通常情况下,要保持一个thread运行,在run方法中通常用一个死循环,例如:while(true){}等。如果满足一定条件,再return或者throw exception跳出循环。
问:有什么方法避免使用死循环而让一个thread保持运行?
这个方法是我研究了3天的tomcat原代码才搞明白的,当时照实佩服了好一阵子。
-jqian(Q_Q);
2002-10-2
{268}
(#778654@0)
-
就照tomcat那样写就可以了. ;-)
-expertune(伪劣钓鱼砖家3磅bass);
2002-10-2
(#778674@0)
-
你真是人如其名啊。:P
-jqian(Q_Q);
2002-10-2
(#778678@0)
-
I'm lazy to use my head.How to do ?
-henhen(哼哼~~,找工ing~~~~);
2002-10-2
(#778686@0)
-
hint: garbage collection
-jqian(Q_Q);
2002-10-3
(#779257@0)
-
Thank you. a pretty good idea. :-P
-henhen(哼哼~~,找工ing~~~~);
2002-10-3
(#779275@0)
-
you can guess how it works just from the hint I gave you? You are sooooo smart.:P
-jqian(Q_Q);
2002-10-3
(#779322@0)
-
I set up a demo to demonstrate your idea. It's ok.
-henhen(哼哼~~,找工ing~~~~);
2002-10-3
(#779958@0)
-
什么叫保持运行?
-sillyboy(silly);
2002-10-2
(#778691@0)
-
我不懂java,但是可以猜猜1,信号量
2,MQ
3,事件
-interview(intervieweree);
2002-10-2
{25}
(#778697@0)
-
Man, Tomcat's GC way is not correct way to run Thread. while(true) is the way to make sure it is wake up and run. finalize cannot garantee the re-active time.I remember tomcat use GC to pool Thread back instead of keep alive.
Maybe I am wrong ... not reading book for years.
-ra_95(小人-盼雪化!等草绿!);
2002-10-3
{119}
(#779433@0)
-
JLS made it very clear that JVM cannot predict when finalize() to be called. Even worse, JVM does not gaurantee finanlize() to be called at all.It is always wrong to put any logic in finanlize() method, other than resource cleanup.
-contact(Contact);
2002-10-3
{87}
(#779987@0)
-
Are there any disadvantages using while(true)?
-yuanzidan(原子弹);
2002-10-3
(#779488@0)
-
GC 如何能保持THREAD运行? 你搞错了吧.
-catorja(Jason);
2002-10-3
(#780402@0)
-
用GC从理论上是可以回收那些已失效的THEAD,但这不是一种好方法,因为GC只是建议SYSTEM进行回收,却不能强制执行.我也有类似while(true)的环境:我的程序是等待客户刷卡的动作,我在while(true)里做了两个出口:(1)侦测到客户刷卡动作(2)时间到了2分钟
-ivy_sh(纸蝶);
2002-10-4
{96}
(#781354@0)
-
你的IO是BLOCK还是UNBLOCK的?
-sillyboy(silly);
2002-10-4
(#781390@0)
-
while(true) is not a good practice. wait(), notify() should be used.
-ray236(ray);
2002-10-4
(#782051@0)