What happens when you call Thread.run() instead of Thread.start() in Java? Trick Interview Question
Learning from mistakes has another name Experience, but if you only learn from your mistakes then there are only limited things you can learn, but if you learn from other people’s mistakes, you can learn much more in a short span of time.
Have you ever thought, Why writing multi-threaded code is difficult? IMHO, the primary reason for this is that it multi-threading makes it hard for a code to speak for itself.
As soon as two threads come into the picture, It becomes very difficult to make a prediction about how your code behaves, especially in the absence of any synchronization rules e.g. rules enforced by the Java Memory Model.
Without JMM you can not make correct predictions about your code in a multi-threaded environment, because it’s possible for one thread to stop at an arbitrary point and another thread at different points.
The situation becomes even more tricky if those threads are sharing data between them e.g. in form of objects, a poorly written multi-threaded program can cause deadlock, race condition, and responsiveness issues, which will prevent a Java application to fulfill its promise.
I hope, in this series we can learn from each other’s mistakes and take a step forward on writing correct multi-threaded applications in Java.
And, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the Java Multithreading, Concurrency, and Performance Optimization course by Michael Pogrebinsy on Udemy. It’s an advanced course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance
Multithreading and Concurrency Mistake in Java – Using run() in place of start()
I am starting with one of the simplest examples, this is a very common mistake by junior programmers and caused by half knowledge. They know that anything written in the run() method of Runnable interface or Thread class will execute in another thread but don’t know how to create another thread in JVM.
Consider the following code :
class KingKong { public static synchronized void main(String[] args) { Thread t = new Thread() { public void run() { kong(); } }; t.run(); System.out.print("King"); } public static synchronized void kong() { System.out.print("Kong"); } }
What Does It Print?
(a) KingKong
(b) KongKing
(c) It varies
(d) Compile time error
We had this question in our Java written test and you will be surprised by the percentage of answers, whopping 50% answers It varies, 10% says compile-time error, another 15% picks answer a KingKong, and the rest of 25% chooses KongKing.
The 50% developer, who chooses It varies, mentioned that there is no guarantee when a thread will start, so it is possible that if the main thread finishes first it will print KongKing and if the new thread executes before the main thread.
Wow, what do you say about these developers, seems a decent lot of programmer who knows some part of multi-threading but overlooked critical detail. The next 10% programmer, who chose Compile time error was unsure whether the main method can be synchronized or not and thought that compiler will not like it.
Next 15% says because “King” comes first in code, it will be printed first and “Kong” will be printed later.
The last 25% who chose “KongKing” are the people who got it correct. We were literally disappointed with these numbers because it wasn’t such a difficult or a tricky question, but I agree sometimes it’s difficult to spot a typo and that’s what makes this error very hard to debug.
Why Code Print KongKing and not KingKong?
The correct answer is “KongKing” and this is because of one typo in the code. The intention of this code is to create a multi-threaded program, but because of t.run() it actually turned into a single-threaded program.
In Java, though it is true that calling Thread.start() will call Runnable.run() method but the complete truth is that calling start() actually creates a new thread and that new thread executes the run() method.
Like in this case, the main thread will execute the run() method first, and thus print “Kong” before coming back and printing “King”, that’s why the output is “KongKing”.
So apart from a typo, this is the key misconception some Java programmer has. This is even more fundamental in nature because it highlights the difference between code and thread.
If you copy paste the above code in Eclipse IDE and debug it you will see the truth, as shown below.
You can see that we have put the breakpoint right at the point where the run() method is called i.e. t.run(). When you step Into this method, you will see that the main thread is executing the run() method and not the new thread.
Now if we just changed the t.run() to t.start(), your program will become multi-threaded and a new thread will be created when the main thread will execute line t.start(), later run() method will be called in this new thread, here is the screenshot of that.
That’s all in the first post of my new series of common Java Multi-threading mistakes. Always use the start() method to start new threads and make your program multi-threaded, don’t call the run() method directly.
The compiler doesn’t prevent you but it creates subtle bugs.
Let me know how do you find this article and don’t forget to share what multi-threading issues you have faced and what lessons you have learned from them. On a closing note, I would share one important tip to understand multi-threading better, debug it.
Yes debugging will tell you how many threads are currently executing your code, you can see their stack trace, values of variables they are holding, and on which lock they are locking. Debugging a multithreaded program is not easy, but once you do it a couple of times, you will find it immensely useful.
- Top 50 Java Thread Interview Questions with Answers (list)
- Top 15 Multithreading and Concurrency Questions from Investment banks (see here)
- 133 Core Java Interview Questions from the last 5 years (see here)
- How volatile variable works in Java? (answer)
- Top 10 Java Concurrency and multi-threading best practices (article)
- Top 5 Courses to learn Multithreading and Concurrency in Java (courses)
- Top 10 Courses to learn Java in-depth (courses)
- 10 Courses to Crack Java Interviews for Beginners (courses)
- Top 5 courses to Learn Java Performance Tuning (courses)
- What is happens-before in Java Concurrency? (answer)
- Top 12 Java Concurrency Questions for Experienced Programmers (see here)
- Top 5 Books to learn Java Concurrency in-depth (books)
- Difference between start() and run() method in Java? (answer)
- 6 Books to learn Multithreading and Concurrency in Java (books)
- 10 Advanced Core Java Courses for Experienced programmers (course)
Thanks for reading this article. If you like this tricky multithreading problem then please share it with your friends and colleagues. If you have any suggestions or feedback then please drop a comment.
you are a Java beginner and want to learn multithreading and concurrency and looking for some free courses to start with then you can also check out this free Java Multithreading course on Udemy. It is a good free course to learn Java Concurrency as well.