Why two threads excecute one by one and not simultaneously?
Can anyone, please, explain, why the following code gives output "Hello Hello Hello Goodbye Goodbye Goodbye" and not "Hello Goodbye Hello Goodbye Hello Goodbye". With current output, first, the h thread is excecuted and only when it is finished, the b thread is excecuted. What am I doing wrong? How can I make the two threads excecute simultaneously? class Hi implements Runnable { public void run(){ for (int i =0;i<3;i++){ System.out.println("Hello"); } } } class Bi implements Runnable { public void run(){ for (int x =0;x<3;x++){ System.out.println("Goodbye"); } } } class MyClass { public static void main(String[ ] args) { Thread h = new Thread(new Hi()); Thread b = new Thread(new Bi()); try { h.sleep(1000); h.start(); } catch (Exception e){ System.out.println(e); } try{ b.sleep (1000); b.start (); } }