Java Multithreading Racers Please write in one class : Your task in this assignm
ID: 3849989 • Letter: J
Question
Java Multithreading Racers
Please write in one class :
Your task in this assignment is to create a threaded class that "races" by counting and displaying the numbers from 1 to 10000. Each of the instances of this thread class should have a unique ID (i.e. the first instance should be numbered "1", the next instance should be numbered "2", etc.).
Now that you have your threaded class, write a main/driver class that instantiates/spawns 10 instances of your threaded class and runs each of them. When the first thread completes and returns, invoke System.exit() to terminate the program; in so doing, you will be able to determine which thread "won" and achieved it's conclusion first.
again please only one class and as simple as possible.
Explanation / Answer
package tuning.threads;
public class ThreadRace
implements Runnable
{
static int num=0;
public static void increment()
{
int n = num;
System.out.print(num+" ");
num = n + 1;
}
public static void main(String args[])
{
ThreadRace d1 = new ThreadRace();
ThreadRace d2 = new ThreadRace();
Thread d1Thread = new Thread(d1);
Thread d2Thread = new Thread(d2);
d1Thread.start();
d2Thread.start();
}
public void run()
{
for (int i = 200; i >= 0 ; i--)
{
increment();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.