Question

Your task in this assignment is to create a threaded class that "races" by counting and...

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.

Homework Answers

Answer #1

JAVA PROGRAM :

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class MyThread extends Thread{
   MyThread(String name){
       super(name);
   }
   @Override
   public void run(){
       for(int i=1;i<=10;i++){
           System.out.println(i+" "+getName());
       }
       System.exit(0);
   }
}
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // Initializing threads
       MyThread threads[] = new MyThread[10];
       for(int i=0;i<10;i++){
           threads[i] = new MyThread("Thread : "+Integer.toString(i));
       }
       //Invoking threads
       for(int i=0;i<10;i++){
           threads[i].start();
       }
   }
}

OUTPUT :

D:\>java Ideone
Thread : 1 - 1
Thread : 1 - 2
Thread : 1 - 3
Thread : 1 - 4
Thread : 1 - 5
Thread : 1 - 6
Thread : 9 - 1
Thread : 9 - 2
Thread : 3 - 1
Thread : 3 - 2
Thread : 3 - 3
Thread : 3 - 4
Thread : 3 - 5
Thread : 3 - 6
Thread : 3 - 7
Thread : 3 - 8
Thread : 3 - 9
Thread : 2 - 1
Thread : 3 - 10
Thread : 9 - 3
Thread : 10 - 1
Thread : 1 - 7

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
Practice using EXCEL – Part of your Orientation Assignment to prepare for class on the first...
Practice using EXCEL – Part of your Orientation Assignment to prepare for class on the first day. Step by Step instructions on completing PR1-5B. BEFORE STARTING TO WORK THE PROBLEM YOU NEED TO WRITE ALL BALANCE FORMULAS. To do so do the following in order. Click on Cell D39. In D39 you will write a formula to add rows D37 and D38. To do so do the following: a)    Start in Cell D39 and press = sign b)    Highlight cell...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...