avatar
suggestions needed# Java - 爪哇娇娃
c*d
1
I am new to Java and have a problem,
I am writing a server program. Whenvever a client connects, it will create a
new thread for that client. The new thread does some work, and goes into
sleep. Every 5 minutes, all the threads wake up and send data out to clients.
How can I let all threads sleep and wake all of them up?
Any suggestions are welcome. thanks.
avatar
r*l
2
wait(), notify(), notifAll().

【在 c**d 的大作中提到】
: I am new to Java and have a problem,
: I am writing a server program. Whenvever a client connects, it will create a
: new thread for that client. The new thread does some work, and goes into
: sleep. Every 5 minutes, all the threads wake up and send data out to clients.
: How can I let all threads sleep and wake all of them up?
: Any suggestions are welcome. thanks.

avatar
H*a
3
read a read javax.concurrent package.

【在 r*****l 的大作中提到】
: wait(), notify(), notifAll().
avatar
c*d
4
found a solution:
all children threads call wait(), master thread setup a timer and loops thro
ugh all children and call interrupt() to wake them up

【在 H***a 的大作中提到】
: read a read javax.concurrent package.
avatar
H*a
5
CountDownLatch in JDK1.5 is exactly what you need I think.
it's in java.util.concurrent package.
There is an example in JDK's JavaDoc.
Sample usage: Here is a pair of classes in which a group of worker threads use two countdown latches:
The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed;
The second is a completion signal that allows the driver to wait until all workers have completed.
class Driver { // ...
void main() throws Inte

【在 c**d 的大作中提到】
: found a solution:
: all children threads call wait(), master thread setup a timer and loops thro
: ugh all children and call interrupt() to wake them up

avatar
c*d
6
thanks for your suggestion, I'll try it out later

【在 H***a 的大作中提到】
: CountDownLatch in JDK1.5 is exactly what you need I think.
: it's in java.util.concurrent package.
: There is an example in JDK's JavaDoc.
: Sample usage: Here is a pair of classes in which a group of worker threads use two countdown latches:
: The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed;
: The second is a completion signal that allows the driver to wait until all workers have completed.
: class Driver { // ...
: void main() throws Inte

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。