This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Beginner Thread Counting Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Beginner Thread Counting Question" Watch "Beginner Thread Counting Question" New topic
Author

Beginner Thread Counting Question

Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Sep 06, 2004
Posts: 41
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

Joined: Aug 07, 2003
Posts: 1646
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
 
subject: Beginner Thread Counting Question
 
Similar Threads
B&S: Testing Locking
KnB notify(), wait(), synchronized block
Daemon Threads
Daemon Thread
Test your business service