c**d 发帖数: 579 | 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. |
r*****l 发帖数: 2859 | 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.
|
H***a 发帖数: 189 | 3 read a read javax.concurrent package.
【在 r*****l 的大作中提到】 : wait(), notify(), notifAll().
|
c**d 发帖数: 579 | 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.
|
H***a 发帖数: 189 | 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
|
c**d 发帖数: 579 | 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
|