What is the output of the following Java snippet? class SampleDemo implements Runnable { private Thread t; private String threadName; SampleDemo (String threadName){ this.threadName = threadName; } public void run() { while (true) System.out.print(threadName); } public void start () { if (t == null) { t = new Thread (this, threadName); t.start (); } }}public class TestThread { public static void main(String args[]) { SampleDemo A = new SampleDemo( "A"); SampleDemo B = new SampleDemo( "B"); B.start(); A.start(); }}
Question
What is the output of the following Java snippet? class SampleDemo implements Runnable { private Thread t; private String threadName; SampleDemo (String threadName){ this.threadName = threadName; } public void run() { while (true) System.out.print(threadName); } public void start () { if (t == null) { t = new Thread (this, threadName); t.start (); } }}public class TestThread { public static void main(String args[]) { SampleDemo A = new SampleDemo( "A"); SampleDemo B = new SampleDemo( "B"); B.start(); A.start(); }}
Solution
The output of this Java code is not deterministic because it involves multithreading. The threads may be scheduled to run in any order by the Java Virtual Machine (JVM), which is dependent on the system and JVM implementation.
However, the output will be a continuous, infinite print of "A" and "B" characters. The exact sequence of "A" and "B" cannot be predicted due to the non-deterministic nature of thread scheduling.
For example, it could be "ABABABABAB..." or "BABAABA..." or any other combination. The program will not terminate on its own because each thread is in an infinite loop.
Similar Questions
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()); }}}
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]
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
correct answerWhat is the name of the thread in the following Java Program?class CT{ public static void main(String args[]) { Thread s = Thread.currentThread(); System.out.println(s); }}
What will be the output of the program?class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }Prints "Inside Thread Inside Thread"Prints "Inside Thread Inside Runnable"Does not compileThrows exception at runtime
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.