Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Simple Thread Client Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Thread Client Question" Watch "Simple Thread Client Question" New topic
Author

Simple Thread Client Question

Michael K. Wilson
Ranch Hand

Joined: Apr 23, 2008
Posts: 45
Hi JavaRanch,

My name is Michael Wilson and am a relative newcomer to Java multi threaded programming... This seems like the place to be for Java beginners!

I wrote a simple greeter program which looks like this:



How do I write simple program which spawns N number of threads? Each thread calls the Greeter in an infinite loop (yield or short pause at end of each iteration).

How do I configure number of threads to control test size?

Also, how do I keep track of the average time to generate each greeting and see how that number changes as load increases?

Michael K. Wilson
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi -

1. Before you go too far, you definitely want to work through the Sun introductory tutorial on threads:

http://java.sun.com/docs/books/tutorial/essential/concurrency/

2. I'll admit, however, that the tutorial gives you way more information than you need just to get started. Here's a simpler example (from Google):

http://scv.bu.edu/Doc/Java/tutorial/java/threads/simple.html

And here is your test program, adapted for the example:

[ April 24, 2008: Message edited by: Paul Santa Maria ]

Paul M. Santa Maria, SCJP
Michael K. Wilson
Ranch Hand

Joined: Apr 23, 2008
Posts: 45
Paul M. Santa Maria,

Thank you for your help!

That's the main reason I posted to this forum because a lot of the tutorials are too comprehensive in a sense.

How do I log the actual time and number of threads it took to produce the greetings to a text file (is this just a matter of doing something like using the java.util.Logging API and placing NSECONDS to stdout)?

Michael K. Wilson
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi -

You've (always) got lots of choices, the answer to the "best solution" is (always) "it depends" :-(

ANYWAY...

As far as "timing", two Pop Favorites include:
1. Using the Java profiler (a *very* useful tool - I encourage you to play with it)

2. Simply put some "currentTimeMilli()" printf's in your code.

Here's an example:
http://www.cs.princeton.edu/introcs/41analysis/Timing.java.html

Your .. PSM

PS:
As far as "logging", I usually use "System.out.println" in console apps, or "log4j" in JSP/J2EE apps. Your suggestion should be just fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Thread Client Question