Why two threads excecute one by one and not simultaneously? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

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

5th May 2019, 8:40 AM
Павел Черныш
5 Answers
+ 6
Here is your code that you are looking for Павел Черныш As Anna suggested i tried to make the same, Have a look at this code i made for you https://code.sololearn.com/cxrPT4T8YpfW/?ref=app NOTE: As SoloLearns codeplayground works on sandbox technique, try executing for more than one time, if you didn't get the output at first sight (tested on my pc ) && (sometimes on sololearn) OUTPUT: Hello Goodbye Hello Goodbye Hello Goodbye Edit: what was happening in your example that once thread was started it was exexuting the for loop and then it was sleeping same for the another thread, so for solution on that i put a Thread.sleep() on a perticular thread process itself, and hence the answer. hope that helped 😄👍
5th May 2019, 10:04 AM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 2
I don't know too much about Java, but it looks like you wait for a second, then call thread 1 which just prints "Hello" three times in a row, wait another second and call thread 2 which prints "Goodbye" three times in a row. There's no reason why "Hello" and "Goodbye" would be printed alternately. Try something like this: Thread 1: print "Hello" wait 500 ms repeat Thread 2: print "Goodbye" wait 500 ms repeat start Thread 1 (maybe pause for a couple of ms) start Thread 2 Alternatively, you can make Thread 1 wait for Thread 2 or vice versa, but I have no idea how to implement that in Java.
5th May 2019, 9:28 AM
Anna
Anna - avatar
+ 2
Simple - the threads execute synchronously rather than asynchronously. The sleep command 😴 ensures this (for the first thread). Moral - Better stay awake 🤗
6th May 2019, 2:06 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Threading works without sleep. It's just that if your program has two threads that run in parallel, the second thread will run as soon as the first thread doesn't have anything better to do and vice versa. This is simulated by letting one thread sleep for a certain amount of time. While one thread is sleeping, the CPU can focus on the other thread. In real life, your program might be waiting for user input in one thread and as long as this thread is waiting for the user to enter something, another thread might be doing all kinds of calculations in the background. Without threading, the whole program would be forced to wait, not doing anything until the user enters something.
5th May 2019, 11:35 AM
Anna
Anna - avatar
0
Aaditya Deshpande Why isn't multithreading working without using sleep() ? I tried this code without any sleep() methods; multithreading was working while running code through windows command prompt, but not in sololearn.
5th May 2019, 10:17 AM
Ashok
Ashok - avatar