New to Threads- Question thread names
Hi everyone, this is my first of probably many posts here.
I am having some issue with some code that I wrote for an assignment. I am learning the use of threads for the first time.
My assignment is to create a random number generator to create a number between 0 and 999, then create and run four threads, each that check 0-249, 250-499, 500-749, and 750-999, displaying the name of whichever thread turns up with the random number. Each thread starts at its low number and checks sequentially. After ten checks, the thread will yield to the next thread, and this will continue until the target random number is found.
My confusion is, when I run my code, depending on what my random number turns out to be, my thread is named "Thread-5, Thread-6, Thread-7, or Thread-8." I am not sure why my numbers aren't ranging from 0-3. Why would it jump to 5-8?
Here is the code:
public class NumberFinderThreadApp
public static void main(String args)
// start Eric Berkowitz's interactive console
int target = (int) (Math.random() * 1000);
System.out.println("The number is " + target);
Thread count1 = new Finder(target,0,249);
Thread count2 = new Finder(target,250,499);
Thread count3 = new Finder(target,500,749);
Thread count4 = new Finder(target,750,999);
class Finder extends Thread
int target, low, high, counter = 0;
public Finder(int target, int low, int high)
this.target = target;
this.low = low;
this.high = high;
public void run()
for (int i = low; i <= high; i++)
if (counter == 10)
counter = 0;
if (i == target)
System.out.println("Target number " + target + " found by " + this.getName());
P.S. I also threw in an output to the console to show me the names of each thread that is instantiated. Here is a picture of the application after it has been run: Screen Shot 2011-07-28 at 4.09.27 PM.png