Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()
Writing multi-threaded as well as concurrent programs is non easy, non fifty-fifty inward Java. Even senior developers, including myself, brand mistakes acre writing concurrent Java applications. This is equally good i of the trickiest surface area of Java programming language, where misconceptions outnumbers concepts. Considering amount of misconception an average Java programmers has well-nigh multi-threading as well as concurrency, I idea to start a novel serial well-nigh common multi-threading mistakes done past times Java programmers; what is improve means to larn from mutual existent discussion mistakes. Learning from mistakes has to a greater extent than or less other holler Experience, but if you lot solely larn from your mistakes as well as then at that spot is solely express things you lot tin flame learn, but if you lot larn from other peoples mistake, you lot tin flame larn much to a greater extent than inward brusque bridge of time. Have you lot always 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 beak for itself. Programmer read code sequentially to empathise how it's executed, but it is solely right if i as well as solely i thread is executing it. That's why Single threaded code are slowly to read as well as debug. As before long equally 2 threads comes into picture, It choke real hard to brand prediction well-nigh how your code behave, particularly inward the absent of whatever synchronization rules e.g. rules enforced past times Java Memory Model. Without JMM you tin flame non brand right prediction well-nigh your code inward a multi-threaded environment, because it's possible for i thread to terminate at arbitrary indicate as well as to a greater extent than or less 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. inward shape of objects, a poorly written multi-threaded computer program tin flame campaign deadlock, race condition as well as responsiveness issues, which volition forbid a Java application to fulfil it's promise. I hope, inward this serial nosotros tin flame larn from each other's error as well as receive got a mensuration frontwards on writing right multi-threaded application inward Java.
Consider next code :
What Does It Print?
(a) KingKong
(b) KongKing
(c) It varies
(d) Compile fourth dimension error
We had this inquiry inward our Java written attempt out as well as you lot volition live on surprised past times the percent of answers, whopping 50% answers It varies, 10% says compile fourth dimension error, to a greater extent than or less other 15% picks reply a, KingKong as well as remainder of 25% chooses KongKing. We equally good enquire to write explanation of why they guide a particular answer, simply to avoid picking somebody 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 starting fourth dimension it volition impress KongKing as well as if novel thread executes earlier principal thread. Wow, what do you lot tell well-nigh these developers, seems a decent lot of programmer who knows to a greater extent than or less business office of multi-threading but overlooked critical detail. The adjacent 10% programmer, who chose Compile fourth dimension error were unsure whether main method tin flame live on synchronized or not as well as idea that compiler volition non like. Next 15% says because "King" comes starting fourth dimension inward code, it volition live on printed starting fourth dimension as well as "Kong" volition live on 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 to a greater extent than or less fourth dimension it's hard to spot a typo as well as that's what makes this error real hard to debug.
You tin flame run into that nosotros receive got position the breakpoint right at the indicate where run() method is called i.e. t.run(). When you lot mensuration Into this method, you lot 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 computer program volition choke multi-threaded as well as a novel thread volition live on created when principal thread volition execute line of piece of occupation t.start(), subsequently run() method volition live on called inward this novel thread, hither is the screenshot of that.
That's all inward starting fourth dimension post service of my novel serial of common Java Multi-threading mistakes. Always purpose start() method to start novel threads as well as brand your computer program multi-threaded, don't telephone vociferation upwards run() method directly. Compiler doesn't forbid you lot but it create subtle bugs. By the way, difference betwixt start() as well as run() method is equally good real mutual inquiry on Java interview. Let me know how do you lot discovery this article as well as don't forget to percentage what multi-threading issues you lot receive got faced as well as what lessons you lot receive got learned from them. On closing note, I would percentage i of import tip to empathise multi-threading better, debug it. Yes debugging volition tell you lot how many threads are currently executing your code, you lot tin flame run into their stack trace, values of variables they are belongings as well as on which lock they are locking. Debugging multi-threaded computer program is non easy, but in i lawsuit you lot do it duet of times, you lot volition discovery it immensely useful.
Further Learning
Multithreading as well as Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Applying Concurrency as well as Multi-threading to Common Java Patterns
Java Concurrency inward Practice Course past times Heinz Kabutz
Using Run Instead of Start
I am starting amongst i of the simplest example, this is real mutual mistakes past times junior programmers as well as caused past times one-half knowledge. They know that anything written inward run() method of Runnable interface or Thread class volition execute inward to a greater extent than or less other thread, but doesn't know how to create to a greater extent than or less other thread inward 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 inquiry inward our Java written attempt out as well as you lot volition live on surprised past times the percent of answers, whopping 50% answers It varies, 10% says compile fourth dimension error, to a greater extent than or less other 15% picks reply a, KingKong as well as remainder of 25% chooses KongKing. We equally good enquire to write explanation of why they guide a particular answer, simply to avoid picking somebody 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 starting fourth dimension it volition impress KongKing as well as if novel thread executes earlier principal thread. Wow, what do you lot tell well-nigh these developers, seems a decent lot of programmer who knows to a greater extent than or less business office of multi-threading but overlooked critical detail. The adjacent 10% programmer, who chose Compile fourth dimension error were unsure whether main method tin flame live on synchronized or not as well as idea that compiler volition non like. Next 15% says because "King" comes starting fourth dimension inward code, it volition live on printed starting fourth dimension as well as "Kong" volition live on 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 to a greater extent than or less fourth dimension it's hard to spot a typo as well as that's what makes this error real hard to debug.
Why Code Print KongKing as well as non KingKong?
Correct reply is "KongKing" as well as this is because of i typo inward code. Intention of this code is to create a multi-threaded program, but because of t.run() it genuinely turned into a unmarried threaded program. In Java, though it is truthful that calling Thread.start() volition telephone vociferation upwards Runnable.run() method but consummate truth is that calling start() genuinely creates a novel thread, as well as that novel thread executes the run() method. If you lot straight telephone vociferation upwards the run() method as well as then no novel thread volition live on created as well as the thread which is running the code volition choke to run() as well as execute it fist as well as and then comeback to it's previous point. Like inward this case, principal thread volition execute run() method first, as well as so impress "Kong" earlier coming dorsum as well as printing "King", that's why output is "KongKing". When I quizzed well-nigh these to to a greater extent than or less programmer who were otherwise expert but got this reply wrong insisted that run() volition telephone vociferation upwards on novel thread because they are calling equally t.run() where t is novel thread object. So apart from typo, this is the key misconception to a greater extent than or less Java programmer has. This is fifty-fifty to a greater extent than key inward 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 nevertheless started because you lot receive got non called the start() method. If you lot copy past times higher upwards code inward Eclipse IDE as well as debug it you lot volition run into the truth, equally shown below.You tin flame run into that nosotros receive got position the breakpoint right at the indicate where run() method is called i.e. t.run(). When you lot mensuration Into this method, you lot 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 computer program volition choke multi-threaded as well as a novel thread volition live on created when principal thread volition execute line of piece of occupation t.start(), subsequently run() method volition live on called inward this novel thread, hither is the screenshot of that.
That's all inward starting fourth dimension post service of my novel serial of common Java Multi-threading mistakes. Always purpose start() method to start novel threads as well as brand your computer program multi-threaded, don't telephone vociferation upwards run() method directly. Compiler doesn't forbid you lot but it create subtle bugs. By the way, difference betwixt start() as well as run() method is equally good real mutual inquiry on Java interview. Let me know how do you lot discovery this article as well as don't forget to percentage what multi-threading issues you lot receive got faced as well as what lessons you lot receive got learned from them. On closing note, I would percentage i of import tip to empathise multi-threading better, debug it. Yes debugging volition tell you lot how many threads are currently executing your code, you lot tin flame run into their stack trace, values of variables they are belongings as well as on which lock they are locking. Debugging multi-threaded computer program is non easy, but in i lawsuit you lot do it duet of times, you lot volition discovery it immensely useful.
Further Learning
Multithreading as well as Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Applying Concurrency as well as Multi-threading to Common Java Patterns
Java Concurrency inward Practice Course past times Heinz Kabutz
0 Response to "Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()"
Post a Comment