File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread concurrency test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread concurrency test" Watch "Thread concurrency test" New topic
Author

Thread concurrency test

Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
I am trying to write a program that could generate a number of threads.. and then wait for a signal to execute some task at the same time.

Here is part of my code:


The problem is... most of CPU power will be taken by kind of infinite loop:


If I insert a sleeping code like this:

Then it isn't launch all the threads at the same time....

Any one got a better idea?


Bryan Lee<br /> <br />SCEA - Sun Certified Enterprise Architect for J2EE Platoform<br />... and couple other certifications.
Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
After review the methods in java.lang.Thread class, I think this is better way:



Anyone got any other ideas?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

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.

Good luck!


[Jess in Action][AskingGoodQuestions]
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
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
Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
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?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a little test. See if it's close ...

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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread concurrency test