Results 1 to 3 of 3
  1. #1
    thecontinuum is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default 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:

    import java.lang.Math;

    public class NumberFinderThreadApp
    {
    public static void main(String args[])
    {
    // start Eric Berkowitz's interactive console
    new eric.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);
    count1.start();
    count2.start();
    count3.start();
    count4.start();
    }

    }

    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;
    }

    @Override
    public void run()
    {
    System.out.println(this.getName());
    for (int i = low; i <= high; i++)
    {
    if (counter == 10)
    {
    counter = 0;
    Thread.yield();
    }

    counter++;
    if (i == target)
    {
    System.out.println("Target number " + target + " found by " + this.getName());
    break;
    }
    }
    }
    }

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    thecontinuum is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Ha! You are totally right, sir. I disabled "Eric's Console" (the eric.jar file I use in Netbeans to have a better looking console) and the thread numbers jumped from 1-4. I am assuming Thread-0 is in use by the JVM? Is that correct?

Similar Threads

  1. Question about 'main thread' and the thread it creates
    By ggyyree in forum Threads and Synchronization
    Replies: 11
    Last Post: 12-10-2010, 08:33 PM
  2. Creating new threads from another thread
    By enzom83 in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-03-2010, 11:15 PM
  3. keep child threads running after parent thread dies
    By adammyth in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 02:43 PM
  4. threads question
    By sandor in forum Threads and Synchronization
    Replies: 9
    Last Post: 02-07-2009, 09:57 PM
  5. how to wrk with twa threads then compile both to 1 thread
    By dmotah in forum Threads and Synchronization
    Replies: 0
    Last Post: 02-04-2008, 10:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •