This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginner Thread Counting Question

 
Mitch Krah
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a multiThread program. The object of the exercise is to create a threaded class that counts/displays to 10,000. Write a main driver that instantiates 10 instances and let them race. The first thread that completes and returns, I am to invoke System.exit() and determine whiech thread won.

My problem is that unless I put a sleep value of 100 or greater I get multiple threads identifying that they were the first to finish before the System.exit is exercised. The following is my code (that works because of the sleep of 100 ms - only counted to 100, because it would take to long to get to 10,000 for results). When the 100 ms sleep is removed I get multiple threads declaring they finished first.

Anyone have any guidance?

class Racers implements Runnable {
int count, n;
Thread thrd;


// Construct a new thread
Racers(String thrdName) {
thrd = new Thread(this, thrdName);
count = 1;
thrd.start(); // start the thread


}

// Begin execution of the new thread
public void run() {
System.out.println("Child Thread " + thrd.getName() + " starting.");
try {
do {
Thread.sleep(100);
System.out.println("In Child Thread " +thrd.getName() + ", count is " + count);
count++;
}while (count <= 100);
System.out.println("Child Thread " + thrd.getName() + " finished first and Won!");
System.exit(0);



}

public class RunRace {
public static void main(String[] args) {

Racers rcr1 = new Racers("1");

Racers rcr2 = new Racers("2");

Racers rcr3 = new Racers("3");

Racers rcr4 = new Racers("4");

Racers rcr5 = new Racers("5");

Racers rcr6 = new Racers("6");

Racers rcr7 = new Racers("7");

Racers rcr8 = new Racers("8");

Racers rcr9 = new Racers("9");

Racers rcr10 = new Racers("10");

}
}
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use synchronization to ensure that only one thread can print the message and call System.exit(). You'll need to choose a shared object on which they can synchronize, but otherwise it's pretty straight forward.
 
Mitch Krah
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think synchronization will meet the intent of the program. The threads are suppose to be "racing" and the first one to finish is to exit the program so that the wining thread can be identified. Synchronization (my understanding) uses monitor and lock to hold a resource until the current thread is complete.

Or, am I not understanding synchronization?

Any additional guidance is appreciated.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that multiple threads are crossing the finish line simultaneously. I think the reason is that the finish line is too long. Instead of a single line (in time), it involves performing several tasks that can theoretically take a long time compared to incrementing a counter.

I believe that when a thread outputs the text saying it is the winner, the fact that it does I/O gives it too much time during which it can be swapped out for another thread. Since the race is so short, this allows a few threads to enter the finish line "zone" before one of them leaves it via System.exit().

So my suggestion is to synchronize crossing the finish line -- not the entire race. The first thread that reaches it should block all others from crossing it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic