Practicing a few examples of java threads, here I create thread A in class A and thread B in class B. Now start these two threads by creating objects. Here I post the code
package com.sri.thread; class A extends Thread { public void run() { System.out.println("Thread A"); for(int i=1;i<=5;i++) { System.out.println("From thread A i = " + i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { System.out.println("Thread B"); for(int i=1;i<=5;i++) { System.out.println("From thread B i = " + i); } System.out.println("Exit from B"); } } public class Thread_Class { public static void main(String[] args) { new A().start();
I did not understand the flow of execution, tried debugging, but did not receive it. Like a thread of execution. This is how I put out what bothers me.
Thread A Thread B End of main thread From thread B i = 1 From thread B i = 2 From thread B i = 3 From thread B i = 4 From thread B i = 5 Exit from B From thread A i = 1 From thread A i = 2 From thread A i = 3 From thread A i = 4 From thread A i = 5 Exit from A
Why does the loop in thread B end before entering the loop in thread A?
user2581076
source share