Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using NetBeans. Create a threaded class that \"races\" by counting and displayin

ID: 3802738 • Letter: U

Question

Using NetBeans. 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.

This is my program so far but I can't get it to work.

CODE:

package Assignement;

public class Assignment {

public class NumberCounter extends Thread {

@Override
public void run(){
  
String currThread = Thread.currentThread().getName();
System.out.println(currThread+" is started");
for(int i=1; i<=10; i++){
System.out.print(i+" ");}
System.out.println(currThread+" is Done, Program exits... "+currThread+" won");
System.exit(MAX_PRIORITY);}
}
public class Assignment Main{
public static void AssignmentString[] args{

NumberCounter thread1 = new NumberCounter();
thread1.setName("1");
NumberCounter thread2 = new NumberCounter();
thread1.setName("2");
NumberCounter thread3 = new NumberCounter();
thread1.setName("3");
NumberCounter thread4 = new NumberCounter();
thread1.setName("4");
NumberCounter thread5 = new NumberCounter();
thread1.setName("5");
NumberCounter thread6 = new NumberCounter();
thread1.setName("6");
NumberCounter thread7 = new NumberCounter();
thread1.setName("7");
NumberCounter thread8 = new NumberCounter();
thread1.setName("8");
NumberCounter thread9 = new NumberCounter();
thread1.setName("9");
NumberCounter thread10 = new NumberCounter();
thread1.setName("10");

thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}}}

Explanation / Answer

This is ypur main class:

package com.Assignment;

public class Assignment {

   public class NumberCounter extends Thread {
       @Override
       public void run(){
      
       String currThread = Thread.currentThread().getName();
       System.out.println(currThread+" is started");
       for(int i=1; i<=10; i++){
       System.out.print(i+" ");}
       System.out.println(currThread+" is Done, Program exits... "+currThread+" won");
       System.exit(MAX_PRIORITY);}
       }
      
}

This is your Driver class:

package com.Assignment;

import com.Assignment.Assignment.NumberCounter;

public class AssignmentMain {

  
  
       public static void main(String[] args){
       Assignment a=new Assignment();
             
           NumberCounter thread1 =a.new NumberCounter();
       thread1.setName("1");
       NumberCounter thread2 = a.new NumberCounter();
       thread1.setName("2");
       NumberCounter thread3 = a.new NumberCounter();
       thread1.setName("3");
       NumberCounter thread4 = a.new NumberCounter();
       thread1.setName("4");
       NumberCounter thread5 = a.new NumberCounter();
       thread1.setName("5");
       NumberCounter thread6 =a. new NumberCounter();
       thread1.setName("6");
       NumberCounter thread7 =a. new NumberCounter();
       thread1.setName("7");
       NumberCounter thread8 =a. new NumberCounter();
       thread1.setName("8");
       NumberCounter thread9 = a.new NumberCounter();
       thread1.setName("9");
       NumberCounter thread10 = a.new NumberCounter();
       thread1.setName("10");
       thread1.start();
       thread2.start();
       thread3.start();
       thread4.start();
       thread5.start();
       thread6.start();
       thread7.start();
       thread8.start();
       thread9.start();
       thread10.start();
       }
}

Output:

Thread-1 is started
10 is started
1   2   3   4   5   6   1   7   2   Thread-2 is started
8   9   10   10 is Done, Program exits... 10 won
Thread-7 is started
1   2   3   4   5