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

You are to explore the Java threads management. You are to test the basic Hello

ID: 3589179 • Letter: Y

Question

You are to explore the Java threads management. You are to test the basic Hello World Threads and then implement a program to compute the sum of n given numbers. Your program should: 1. Instantiate 2 different java Threads saying “ Hello World, I am in thread # 1” or “ Hello World, I am in thread # 2”

class ThreadTest extends Thread {

   private Thread t;  

private String threadName;  

      ThreadTest( String name){   

     threadName = name;  

      System.out.println("Creating " + threadName );  

}

   public void run() {

      System.out.println("Running Thread:.. " );   

    try {       

   for(int i = 4; i > 0; i--) {      

       System.out.println("Thread: " + threadName + ", " + i);   

          // Let the thread sleep for a while.     

        Thread.sleep(500);      

    }   

   } catch (InterruptedException e) {   

   + " interrupted.");   

   }     

System.out.println("Thread " + threadName + " exiting.");

   }    

    public void start ()   

{     

System.out.println("Starting " + threadName );  

     if (t == null)     

{       

   t = new Thread (this, threadName);   

       t.start ();    

   }

   }

}

public class TestTest {  

public static void main(String args[]) {   

        ThreadTest T1 = new ThreadTest( "Thread-1");  

     T1.start();        

      ThreadTry T2 = new ThreadTry( "Thread-2");    

   T2.start();  

}   

}

Then Modify the java Thread code to compute the sum of n numbers.

Explanation / Answer

//code to print the hello world ii am in thread

class ThreadTest extends Thread {

private Thread t;  

private String threadName;  

ThreadTest( String name){

this.threadName = name;

t = new Thread(this.threadName);

}

public void run() {   

System.out.println("Hello World, I am in "+t.getName());

}

}

public class TestTest {  

public static void main(String args[]) {

  

ThreadTest T1 = new ThreadTest( "Thread-1");  

T1.start();   

ThreadTest T2 = new ThreadTest( "Thread-2");   

T2.start();  

}

}

// code to find the sum of n integers

import java.util.Scanner;

class ThreadTest extends Thread {

private Thread t;  

private String threadName;  

ThreadTest( String name){

this.threadName = name;

t = new Thread(this.threadName);

}

public void run() {   

Scanner sc = new Scanner(System.in);

int num=0, sum=0;

System.out.println("Enter any integer for which you want to find sum");

num = sc.nextInt();

for(int i=1;i<=num;i++)

sum = sum + num;

System.out.println("Sum of "+ num +" integers is "+ sum);

}

}

public class TestTest {  

public static void main(String args[]) {

  

ThreadTest T1 = new ThreadTest( "Thread-1");  

T1.start();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote