Knowee
Questions
Features
Study Tools

Find the output of the below code:public class CN{public static void main(String args[]) { Thread startThread1 = new Thread(new Output("start1")); Thread runThread = new Thread(new Output("run")); Thread startThread2 = new Thread(new Output("start2")); startThread2.start(); startThread1.start(); runThread.run(); }private static class Output implements Runnable{ private String str; public Output(String str){ this.str = str; } @Override public void run() { System.out.println(str + " is executed by Thread : " + Thread.currentThread().getName()); }}}

Question

Find the output of the below code:public class CN{public static void main(String args[]) { Thread startThread1 = new Thread(new Output("start1")); Thread runThread = new Thread(new Output("run")); Thread startThread2 = new Thread(new Output("start2")); startThread2.start(); startThread1.start(); runThread.run(); }private static class Output implements Runnable{ private String str; public Output(String str){ this.str = str; } @Override public void run() { System.out.println(str + " is executed by Thread : " + Thread.currentThread().getName()); }}}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code is not deterministic because the execution order of threads in Java is decided by the thread scheduler, which is part of the JVM. The thread scheduler's behavior is not predictable and can vary from run to run.

However, the runThread.run(); will always execute in the main thread because the run() method is called directly, which doesn't create a new thread.

So, one possible output could be:

start2 is executed by Thread : Thread-0
start1 is executed by Thread : Thread-1
run is executed by Thread : main

But remember, the first two lines could be in any order. The last line will always be the same because it's executed in the main thread.

This problem has been solved

Similar Questions

Find the output of the below program?class MyRunnable implements Runnable { public void run() { System.out.println("Child thread"); }}public class Test { public static void main(String[] args) { MyRunnable r = new MyRunnable(); Thread t1 = new Thread(); Thread t2 = new Thread(r); t1.start(); }}A) Child ThreadB) No OutputC) Compile time errorD) Exception

Select the correct answerWhat will be the output of the following Java code?class newthread extends Thread { Thread T; newthread() { T = new Thread(this,"T1"); T.start(); } public void run() { try { T.join() System.out.println(T.getName()); } catch(Exception e) { System.out.print("Exception"); } } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }OptionsExceptionThread[My Thread,5,main] My ThreadRuntime Error

Select the correct answerWhat will be the output of the following Java program?class newthread implements Runnable   { Thread var1,var2;    newthread()     {      var1 = new Thread(this,"Thread_1");      var2 = new Thread(this,"Thread_2");      var1.start();      var2.start(); } public void run()     {      var2.setPriority(Thread.MAX_PRIORITY);   System.out.print(var1.equals(var2));    }     }  class Multithreaded_programing   {    public static void main(String args[])     {      new newthread();         }  }Optionsfalsetruetruetruefalsefalse

Select the correct answerWhat is the output in the following Java program?class Main{  public static void main(String args[])  {    Thread t = Thread.currentThread();    System.out.println(t.isAlive());       }}Options0truefalse1

What will be the output of the following Java code?class multithreaded_programing { public static void main(String args[]) {Thread t = Thread.currentThread();System.out.println(t); } } Thread[5,main]Thread[main,5]Thread[main,0]Thread[main,5,main]

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.