File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread concurrency test

 
Bryan Lee
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic