![]() ![]() If another thread tries to execute the same method then it will go to the BLOCKED state, until the first thread completes executing the method. When a method is synchronized, only one thread who has the lock of that object can execute that method. If you notice both ‘CoolObject#method1()’ and ‘HotObject#method2()’ are synchronized methods. Similarly ‘run()’ method in ‘ThreadB’ invokes ‘HotObject#method2()’. ‘run()’ method in ‘ThreadA‘ invokes ‘CoolObject#method1()’. In this method, 2 threads with the name ‘ThreadA’ and ‘ThreadB’ are launched. You can notice the sample program contains the ‘DeadlockApp’ class. Here is a sample program application, which generates deadlock between 2 kotlin threads. The only way to recover from Deadlock is to restart the application. Once a Deadlock happens in the application, it cannot be recovered. On the other hand, when Train-B is in part-4, it will be stuck waiting for part-3, which Train-A holds. Because when Train-A is in part-3 of the train track, it will be stuck waiting for part-4 of the track, which Train-B holds. Under this circumstance, Train-A and Train-B will reach a Deadlock state when they reach part-3 and part-4 of the train track. Let’s say both trains travel at the same speed. Train-A starts at part-1 and Train-B starts at Part-6 on the same train track at the same time. ![]() Let’s say there is only one train track, and this train track has six parts(part-1, part-2, part-3, part-4, part-5, part-6). Let’s look at the below practical example, which may help you to understand Deadlock better. Deadlock’s definition goes like this: “Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.” It’s always easier to learn something new through examples and pictures. Several technical definitions aren’t clear. In this post let’s discuss how to simulate a deadlock in Kotlin and how you can troubleshoot it.įirst let’s try to understand what ‘Deadlock’ means. Modern Kotlin platform is no exception to it. All multi-threaded applications are prone to deadlock. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |