Multithreading \"Racers\" Now that you\'ve completed the unit on multithreading,
ID: 3730429 • Letter: M
Question
Multithreading "Racers" Now that you've completed the unit on multithreading, you know the basics of how to create and run thread-based classes. This assignment allows you to practice multithreading programming. Your task in this assignment is to create a threaded class that "races" by counting and displaying the numbers from 1 to 10. 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.
Explanation / Answer
Answer.)
Code for Racer Multi-threading :
class Racer extends Thread {
private Thread t;
private String racerName;
Racer( String name) {
//Initialising racer name
racerName = name;
}
//Calling the run method
public void run() {
boolean complete=false;
try {
for(int i = 1; i <=10; i++) {
System.out.println("Racer: " + racerName + "--" + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
complete=true;
}catch (InterruptedException e) {
System.out.println("Racer " + racerName + " interrupted.");
}
//Checking if loop completes once, printing and exiting
if(complete==true){
System.out.println("Racer " + racerName + " complete.");
System.exit(0);
}
}
//Starting the thread
public void start () {
if (t == null) {
t = new Thread (this, racerName);
t.start ();
}
}
}
public class TestThread {
public static void main(String args[]) {
//Creating thread objects
Racer T1 = new Racer("1");
Racer T2 = new Racer("2");
Racer T3 = new Racer("3");
Racer T4 = new Racer("4");
Racer T5 = new Racer("5");
Racer T6 = new Racer("6");
Racer T7 = new Racer("7");
Racer T8 = new Racer("8");
Racer T9 = new Racer("9");
Racer T10 = new Racer("10");
T1.start();
T2.start();
T3.start();
T4.start();
T5.start();
T6.start();
T7.start();
T8.start();
T9.start();
T10.start();
}
}
Output :
Racer: 1--1
Racer: 2--1
Racer: 4--1
Racer: 5--1
Racer: 3--1
Racer: 6--1
Racer: 7--1
Racer: 8--1
Racer: 9--1
Racer: 10--1
Racer: 1--2
Racer: 2--2
Racer: 4--2
Racer: 5--2
Racer: 3--2
Racer: 6--2
Racer: 7--2
Racer: 8--2
Racer: 9--2
Racer: 10--2
Racer: 1--3
Racer: 2--3
Racer: 4--3
Racer: 3--3
Racer: 5--3
Racer: 6--3
Racer: 7--3
Racer: 8--3
Racer: 9--3
Racer: 10--3
Racer: 1--4
Racer: 2--4
Racer: 4--4
Racer: 3--4
Racer: 5--4
Racer: 6--4
Racer: 7--4
Racer: 8--4
Racer: 9--4
Racer: 10--4
Racer: 1--5
Racer: 2--5
Racer: 4--5
Racer: 3--5
Racer: 5--5
Racer: 6--5
Racer: 7--5
Racer: 8--5
Racer: 9--5
Racer: 10--5
Racer: 1--6
Racer: 2--6
Racer: 4--6
Racer: 3--6
Racer: 5--6
Racer: 6--6
Racer: 7--6
Racer: 8--6
Racer: 9--6
Racer: 10--6
Racer: 1--7
Racer: 2--7
Racer: 4--7
Racer: 3--7
Racer: 5--7
Racer: 6--7
Racer: 7--7
Racer: 8--7
Racer: 9--7
Racer: 10--7
Racer: 1--8
Racer: 2--8
Racer: 4--8
Racer: 3--8
Racer: 5--8
Racer: 6--8
Racer: 7--8
Racer: 8--8
Racer: 9--8
Racer: 10--8
Racer: 1--9
Racer: 2--9
Racer: 4--9
Racer: 3--9
Racer: 5--9
Racer: 6--9
Racer: 7--9
Racer: 8--9
Racer: 9--9
Racer: 10--9
Racer: 1--10
Racer: 2--10
Racer: 4--10
Racer: 3--10
Racer: 5--10
Racer: 6--10
Racer: 7--10
Racer: 8--10
Racer: 9--10
Racer: 10--10
Racer 2 complete.
Racer 4 complete.
Racer 1 complete.
Racer 3 complete.
For any doubts, drop a comment below.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.