Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Thread Client Question

 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic