Results 1 to 3 of 3
- 07-28-2011, 10:08 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
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
-
The smaller numbered threads are already in use -- by your program and the JVM.
- 07-28-2011, 10:13 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Question about 'main thread' and the thread it creates
By ggyyree in forum Threads and SynchronizationReplies: 11Last Post: 12-10-2010, 07:33 PM -
Creating new threads from another thread
By enzom83 in forum Threads and SynchronizationReplies: 1Last Post: 12-03-2010, 10:15 PM -
keep child threads running after parent thread dies
By adammyth in forum Threads and SynchronizationReplies: 2Last Post: 01-27-2010, 01:43 PM -
threads question
By sandor in forum Threads and SynchronizationReplies: 9Last Post: 02-07-2009, 08:57 PM -
how to wrk with twa threads then compile both to 1 thread
By dmotah in forum Threads and SynchronizationReplies: 0Last Post: 02-04-2008, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks