- 1
Java program doubt
Can someone pls explain THIS PROGRAM How many lines of output will this code produce? class B implements Runnable { public void run() { System.out.println("B"); } } class A extends Thread { public void run() { System.out.println("A"); Thread t = new Thread(new B()); t.start(); } public static void main(String[ ] args) { A object = new A(); object.start(); } }
2 Answers
+ 3
Why do you want it to be explained...?
0
This will output two lines.
A
B
When you say object.start() a new thread is created and the run method inside class A gets called. It first prints 'A' then inside you are creating a thread of class B.
So t.start() will again create a new thread and it will call the run method of class B which prints 'B'.
In total there are 3 threads including the main thread.