Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()
Tuesday, July 24, 2018
Add Comment
Writing multi-threaded as well as concurrent programs is non easy, non fifty-fifty inwards Java. Even senior developers, including myself, brand mistakes spell writing concurrent Java applications. This is too 1 of the trickiest expanse of Java programming language, where misconceptions outnumbers concepts. Considering amount of misconception an average Java programmers has most multi-threading as well as concurrency, I idea to start a novel serial most common multi-threading mistakes done yesteryear Java programmers; what is ameliorate agency to larn from mutual existent give-and-take mistakes. Learning from mistakes has approximately other yell Experience, but if yous solely larn from your mistakes as well as then in that place is solely express things yous tin learn, but if yous larn from other peoples mistake, yous tin larn much to a greater extent than inwards brusque bridge of time. Have yous e'er thought, Why writing multi-threaded code is difficult? IMHO, primarily argue for this is that it multi-threading makes it hard for a code to utter for itself. Programmer read code sequentially to empathise how it's executed, but it is solely right if 1 as well as solely 1 thread is executing it. That's why Single threaded code are tardily to read as well as debug. As before long every bit 2 threads comes into picture, It acquire rattling hard to brand prediction most how your code behave, specially inwards the absent of whatever synchronization rules e.g. rules enforced yesteryear Java Memory Model. Without JMM you tin non brand right prediction most your code inwards a multi-threaded environment, because it's possible for 1 thread to cease at arbitrary signal as well as approximately other thread at dissimilar point. Situation becomes fifty-fifty to a greater extent than tricky if those threads are sharing information betwixt them e.g. inwards shape of objects, a poorly written multi-threaded programme tin crusade deadlock, race condition as well as responsiveness issues, which volition forestall a Java application to fulfil it's promise. I hope, inwards this serial nosotros tin larn from each other's error as well as receive got a mensuration frontward on writing right multi-threaded application inwards Java.
Consider next code :
What Does It Print?
(a) KingKong
(b) KongKing
(c) It varies
(d) Compile fourth dimension error
We had this enquiry inwards our Java written examine as well as yous volition hold out surprised yesteryear the pct of answers, whopping 50% answers It varies, 10% says compile fourth dimension error, approximately other 15% picks reply a, KingKong as well as remainder of 25% chooses KongKing. We too inquire to write explanation of why they pick out a particular answer, simply to avoid picking mortal who is guessing their way. The 50% developer, who chooses It varies, mentioned that there is no guarantee when a thread volition start, so it possible that if main thread finishes outset it volition impress KongKing as well as if novel thread executes earlier principal thread. Wow, what do yous tell most these developers, seems a decent lot of programmer who knows approximately business office of multi-threading but overlooked critical detail. The side yesteryear side 10% programmer, who chose Compile fourth dimension error were unsure whether main method tin hold out synchronized or not as well as idea that compiler volition non like. Next 15% says because "King" comes outset inwards code, it volition hold out printed outset as well as "Kong" volition hold out printed later. The Last 25% who chose "KongKing" are the people who got it correct. We were literally disappointed amongst these numbers because it wasn't such a hard of tricky question, but I concur approximately fourth dimension it's hard to topographic point a typo as well as that's what makes this error rattling hard to debug.
You tin run into that nosotros receive got seat the breakpoint right at the signal where run() method is called i.e. t.run(). When yous mensuration Into this method, yous volition run into that main thread is executing run() method as well as non the new thread. Now if nosotros simply changed the t.run() to t.start(), your programme volition acquire multi-threaded as well as a novel thread volition hold out created when principal thread volition execute work t.start(), subsequently run() method volition hold out called inwards this novel thread, hither is the screenshot of that.
That's all inwards outset ship of my novel serial of common Java Multi-threading mistakes. Always role start() method to start novel threads as well as brand your programme multi-threaded, don't telephone telephone run() method directly. Compiler doesn't forestall yous but it create subtle bugs. By the way, difference betwixt start() as well as run() method is too rattling mutual enquiry on Java interview. Let me know how do yous notice this article as well as don't forget to portion what multi-threading issues yous receive got faced as well as what lessons yous receive got learned from them. On closing note, I would portion 1 of import tip to empathise multi-threading better, debug it. Yes debugging volition tell yous how many threads are currently executing your code, yous tin run into their stack trace, values of variables they are belongings as well as on which lock they are locking. Debugging multi-threaded programme is non easy, but 1 time yous do it duo of times, yous volition notice it immensely useful.
Further Learning
Multithreading as well as Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
Applying Concurrency as well as Multi-threading to Common Java Patterns
Java Concurrency inwards Practice Course yesteryear Heinz Kabutz
Sumber https://javarevisited.blogspot.com/
Using Run Instead of Start
I am starting amongst 1 of the simplest example, this is rattling mutual mistakes yesteryear junior programmers as well as caused yesteryear one-half knowledge. They know that anything written inwards run() method of Runnable interface or Thread class volition execute inwards approximately other thread, but doesn't know how to create approximately other thread inwards JVM.Consider next 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 fourth dimension error
We had this enquiry inwards our Java written examine as well as yous volition hold out surprised yesteryear the pct of answers, whopping 50% answers It varies, 10% says compile fourth dimension error, approximately other 15% picks reply a, KingKong as well as remainder of 25% chooses KongKing. We too inquire to write explanation of why they pick out a particular answer, simply to avoid picking mortal who is guessing their way. The 50% developer, who chooses It varies, mentioned that there is no guarantee when a thread volition start, so it possible that if main thread finishes outset it volition impress KongKing as well as if novel thread executes earlier principal thread. Wow, what do yous tell most these developers, seems a decent lot of programmer who knows approximately business office of multi-threading but overlooked critical detail. The side yesteryear side 10% programmer, who chose Compile fourth dimension error were unsure whether main method tin hold out synchronized or not as well as idea that compiler volition non like. Next 15% says because "King" comes outset inwards code, it volition hold out printed outset as well as "Kong" volition hold out printed later. The Last 25% who chose "KongKing" are the people who got it correct. We were literally disappointed amongst these numbers because it wasn't such a hard of tricky question, but I concur approximately fourth dimension it's hard to topographic point a typo as well as that's what makes this error rattling hard to debug.
Why Code Print KongKing as well as non KingKong?
Correct reply is "KongKing" as well as this is because of 1 typo inwards code. Intention of this code is to create a multi-threaded program, but because of t.run() it truly turned into a unmarried threaded program. In Java, though it is truthful that calling Thread.start() volition telephone telephone Runnable.run() method but consummate truth is that calling start() truly creates a novel thread, as well as that novel thread executes the run() method. If yous direct telephone telephone the run() method as well as then no novel thread volition hold out created as well as the thread which is running the code volition conk to run() as well as execute it fist as well as and then comeback to it's previous point. Like inwards this case, principal thread volition execute run() method first, as well as hence impress "Kong" earlier coming dorsum as well as printing "King", that's why output is "KongKing". When I quizzed most these to approximately programmer who were otherwise skillful but got this reply wrong insisted that run() volition telephone telephone on novel thread because they are calling every bit t.run() where t is novel thread object. So apart from typo, this is the key misconception approximately Java programmer has. This is fifty-fifty to a greater extent than commutation inwards nature because it highlight difference betwixt code as well as thread. Here definitely run() is called on t, which is a novel thread, but the thread which is executing code is non thread t, but main thread. t is non all the same started because yous receive got non called the start() method. If yous copy yesteryear higher upward code inwards Eclipse IDE as well as debug it yous volition run into the truth, every bit shown below.You tin run into that nosotros receive got seat the breakpoint right at the signal where run() method is called i.e. t.run(). When yous mensuration Into this method, yous volition run into that main thread is executing run() method as well as non the new thread. Now if nosotros simply changed the t.run() to t.start(), your programme volition acquire multi-threaded as well as a novel thread volition hold out created when principal thread volition execute work t.start(), subsequently run() method volition hold out called inwards this novel thread, hither is the screenshot of that.
That's all inwards outset ship of my novel serial of common Java Multi-threading mistakes. Always role start() method to start novel threads as well as brand your programme multi-threaded, don't telephone telephone run() method directly. Compiler doesn't forestall yous but it create subtle bugs. By the way, difference betwixt start() as well as run() method is too rattling mutual enquiry on Java interview. Let me know how do yous notice this article as well as don't forget to portion what multi-threading issues yous receive got faced as well as what lessons yous receive got learned from them. On closing note, I would portion 1 of import tip to empathise multi-threading better, debug it. Yes debugging volition tell yous how many threads are currently executing your code, yous tin run into their stack trace, values of variables they are belongings as well as on which lock they are locking. Debugging multi-threaded programme is non easy, but 1 time yous do it duo of times, yous volition notice it immensely useful.
Further Learning
Multithreading as well as Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
Applying Concurrency as well as Multi-threading to Common Java Patterns
Java Concurrency inwards Practice Course yesteryear Heinz Kabutz
0 Response to "Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()"
Post a Comment