Deadlock from Multithreading

INTRODUCTION

The objective of this small article is to give an introduction about threads, deadlocks and how a dead lock may occur illustrated with code using the Java programming language. At the end, I give my personal point of view about the subject.

MULTITREADING

A thread can be defined simply by “a separated computation process” that may be a children of other threads – or a main program (Savitch, 2010). Threads can also operate in a parallel manner, what means multiple computation processes sharing 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.

deadlockFigure 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.

 deadlock2

Figure 2 Example of a Multithreading application in Java

When running the code above, will give the output of:

:/

:/

Is off

:/

Is on

Is off

Is on

Is off

Is on

Is off

:/

Table 1 Ouptut of the program

DEADLOCK

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.”

(Goetz, 2001)

The Java programming language offers synchronization resources in order to prevent dead locks. 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:

deadlock5

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.

CONCLUSION

Deadlocks are a very sensible threat to every program which uses multithreading programs sharing the same resources. While debugging and simulating dead locks are difficult because it needs timing and also it may not occur because of 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.

REFERENCES

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. 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].

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s