Question

Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingD
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following output for the given program.

Explanation:

1. Its a multithreading program in java which creates three threads and all the three threads share the variable countDown.

2. Here all the three threads will be running concurrently and its very hard to determine the order of thread execution.

3. In this program we can judge only the possible outcomes/display in the program when the program is executed.

4. Only the values 9.8.7 will be the output but the order in which thread executed will change the display.

5. For example, if the thread 1, first runs, its value is 9 and if the thread 0 might have decremented the countDown value to 8 and the thread 2 would have got time slice to executed which will decrement again and print 7 before 8.

Possible Outputs:

9

8

7

and

9

7

8

Modified program to understand which thread runs in which order and its value:

import java.util.Random;

class MultithreadingDemo extends Thread{
public void run(){
try{
//added the following line to understand how thread runs, instead of line#8
System.out.println("Name: " + getName() +" and countDown values is " + --MultiThread.countDown);
//System.out.println(--MultiThread.countDown);

}
catch(Exception e){
  
}
}
}
public class MultiThread
{

//countDown is shared between the 3 threads
public static int countDown=10;
public static void main(String[] args) {
   for (int i=0; i<3;i++){
   MultithreadingDemo object = new MultithreadingDemo();
   object.start();
   }
   }
}

Following different executions shows different order of thread execution.

Output 1:

Name: Thread-0 and countDown values is 9
Name: Thread-2 and countDown values is 7
Name: Thread-1 and countDown values is 8


Output 2:

Name: Thread-1 and countDown values is 9
Name: Thread-0 and countDown values is 8
Name: Thread-2 and countDown values is 7


Output 3:

Name: Thread-0 and countDown values is 9
Name: Thread-1 and countDown values is 8
Name: Thread-2 and countDown values is 7


Note: for example assume any of 3 threads started its first execution and decremented the countDown value to 9 but it didnt print to console, mean while if anyother 2 threads started execution and decremented the value to 8 and prints immediately to console, then there is chance that 8 prints to console before 9. then the following output is possible.

8

9

7.

In multithreading environment we cant ensure the order of outputs but the possible outputs could always be there.

As i mentioned in "Note" following are the possible outputs of the given program based on thread execution.

9 8 7

9 7 8

7 9 8

7 8 9

8 9 7

8 7 9

Screen shot:

} بہا 3 4. class MultithreadingDemo extends Thread{ 5. public void run() { 6 try{ 7 //added the following line to understand

Add a comment
Know the answer?
Add Answer to:
Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program:...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • Java questions QUESTION 8 Assume the following classes are each defined in their own java files...

    Java questions QUESTION 8 Assume the following classes are each defined in their own java files and compile public class A public void display { System.out.println("A's display"); public class B extends A{ public void display { System.out.println("B's display"); public class C extends A public void display { System.out.println("C's display"); public class D { public static void main(String args) { 3: int i = ((int)(Moth.random(* 10)) if (i == 0) a = new BO: else if (i == 1) a =...

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

  • What is the output for the following program codes? a) [ 0.5 Mark ] class A...

    What is the output for the following program codes? a) [ 0.5 Mark ] class A { int i; } class B extends A { int j; void display() { super.i = j + 1; System.out.println(j + " " + i); }} class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); }} OUTPUT: b) [ 0.5 Mark ] class Parent { public void getBike(){ System.out.println("Suzuki Bike"); }} class Child extends Parent {...

  • java Use the classes given in the previous question and write the output tha t is...

    java Use the classes given in the previous question and write the output tha t is generated when this program is run. You may write your answer as "Elaine 2 Jerry 1" or "Compiler Error" or "Runtime Error" If there is an error then explain why. public class Main{ public static void main(String args[]){ Object o = new Kramer(); ((George)(o)).method1(); ((Jerry)(o)). method1(); 0.method2(); public class George extends Elaine {! public void method1() { System.out.print("George 1 "); public class Jerry {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT