- 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(); } }

26th Nov 2020, 4:46 PM
Pranay Pandey
Pranay Pandey - avatar
2 Antworten
+ 3
Why do you want it to be explained...?
26th Nov 2020, 4:56 PM
Steve Sajeev
Steve Sajeev - avatar
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.
26th Nov 2020, 7:35 PM
Avinesh
Avinesh - avatar