First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!
Now, as to your question: you want to use wait() and notify(). This is the correct way for Threads to communicate in Java. It allows one thread to wait without using any CPU time, and then instantly wake up when another thread wants it to.
This is a bit involved to explain here, so it's best if you learn about it from a primary source. The Sun tutorial on Threads has all the information you'll need. In the particular, the lessons on "Synchronizing Threads" and "Thread Pools" will give you the specifics you're after -- but you should probably read the whole thing.
You may want to have a close look at the wait/notify mechanism in java. It would take some reading, but it appears to be perfect for the situaltion you describe
Hello Ernest Friedman-Hill, First of all, I have modified my name according to the naming policy ;-) And thanks for welcoming!
I have also read the tutorial and realize there is a new java.util.concurrency package in Java 5. The package seems very useful for creating thread pool! My new program is now look like this:
However, the invokeAll() method just like a black box to me. As I am looking for a better way to run all of the thread AT THE SAME TIME, anyone know how was it implemented inside this mehtod?
A wild guess/comment is helpful as well! [ May 24, 2005: Message edited by: Shining Liao ]
Ernest Friedman-Hill
,
author and iconoclast
staff
Note that all of the source for the Java APIs is freely available -- in fact, it comes with the JDK download and you get a chance to install it when you install the JDK. Look in your java installation directory for a file named "src.zip" containing this source.
But the answer to your question is that it's just done with a loop. Just one thread is started at a time.
Originally posted by Sol Mam-Orn: You may want to have a close look at the wait/notify mechanism in java. It would take some reading, but it appears to be perfect for the situaltion you describe
Thank you for the suggestion!
I have look into the detail of wait/notify/notifyAll, but it isn't exactly what I am looking for. This mechanism designed for synchronization control over a contention resource. I could use notifyAll to wake up all threads at the same time but then only one thread who won the resource contention can carry on...
What I want is wake up all threads and carry on doing their jobs at the same time without a resource contention monitor exist.
Anyway, I am kind of giving up this thought. On the other hand, java is kind of thread safe.. isn't it?
and output ... note that the three worker threads are working at the same time ...
The M: Working delay is there because it's important for the worker threads to get to the wait() before the main does its synchronize. [ May 25, 2005: Message edited by: Stan James ]
Post by:autobot
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards