The objective of this small article is to give an introduction about threads, deadlocks and how a deadlock may occur illustrated with code using the Java programming language. In the end, I give my personal point of view about the subject.
A thread can be defined simply by “a separated computation process” that may be children of other threads – or the main program (Savitch, 2010). Threads can also operate in a parallel manner, what means multiple computation processes sharing the same resources, and running for a common goal.
In Java, this can be achieved by using the Thread class, which offers support for implementing single and multithread applications.
Figure 1 Illustration of a Thread Program in Java
The code figure above shows a simple thread program coded up with the Java language. Basically, this illustrates one thread instanced by the Thread class. The ThreadExample1 class is just an implementation of Runnable, one of the ways of implementing threads with the Java programming language.
Figure 2 Example of a Multithreading application in Java
When running the code above, will give the output of:
Table 1 Output of the program
Deadlock is defined by WhatIs.com, 2016 as “A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function.”
Figure 3 An illustration of a dead lock
The figure above illustrates a deadlock occurring on the traffic. What happens is that all the cars are trying to access the same resource at the same time, what locks the traffic to flow.
DEADLOCK IN JAVA
“When in doubt, synchronize.”
The Java programming language offers synchronization resources in order to prevent deadlocks. What happens is that at every variable that is overwritten using multiple threads, it is strictly recommended that you use synchronization.
Example of a potential synchronization deadlock:
Figure 4 Using the synchronization resource (Goetz, 2001)
Image a scenario while thread A calls oneMethod(), while thread B calls anotherMethod(). What happens is that thread A gets the lock on cacheLock and at the same time thread b gets the lock on tableLock. Now the threads are deadlocked.
TIPS TO PREVENT DEADLOCKS IN JAVA
According to the StackOverflow, 2016 forum, some good tips to avoid deadlocks are:
Always use lock-free data-structures;
Release locks after a timeout period;
Always acquire the locks in the same order.
Deadlocks are a very sensible threat to every program which uses multithreading programs sharing the same resources. While debugging and simulating deadlocks are difficult because it needs timing and also it may not occur because of the environment and machine load. The Java programming language offers resources to deal with deadlock issues, however, it is more of a programmers responsibility to avoid this problem.
Savitch, W, 2013. Absolute Java. 5th ed. USA: Pearson.
WhatIs.com. 2016. What is deadlock? – Definition from WhatIs.com. [ONLINE] Available at: http://whatis.techtarget.com/definition/deadlock. [Accessed 01 May 2016].
Blogspot. 2016. A figure of a deadlock. [ONLINE] Available at: http://3.bp.blogspot.com/-Whe-8HEWtRs/UAJwpICqPjI/AAAAAAAAAeQ/4XFtKnj1-JE/s1600/Comxf.jpeg. [Accessed 1 May 2016].
Brian Goetz. 2001. Avoid synchronization deadlocks | JavaWorld. [ONLINE] Available at: http://www.javaworld.com/article/2075692/java-concurrency/avoid-synchronization-deadlocks.html. [Accessed 01 May 2016].
Multithreading – Tips to prevent deadlocks in java. 2016. Stack Overflow. [ONLINE] Available at: http://stackoverflow.com/questions/16780937/tips-to-prevent-deadlocks-in-java. [Accessed 01 May 2016].