Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Best Practices for Multithreading in Java

Get a glimpse at the basics of threads and multithreading and the best practices that should be adopted when working with multithreading in enterprise applications.


advertisement

A thread is a path of execution within a process. Multithreading is the ability to improve the overall throughput of an application by providing a way to execute many threads simultaneously. This article presents a discussion on the best practices that should be adopted when working with multi-threading in enterprise applications.

What is multithreading and why is it useful?

Multithreading may be defined as the ability to have multiple threads in memory with one of them in the execution state. Threads share the resources of a process that includes memory resources, process data and open files. This, though it helps to make your application more efficient, might cause issues at runtime due to concurrent access to the same resource. Hence, you should be aware of the best practices that should be followed when working with multithreading in Java.

Using the Java Executor Framework

The Java Executor Framework was introduced in Java 1.5 and is contained inside the Java concurrency package. It provides an abstraction over the Java threading framework provided by the core Java runtime. It should be noted that creation of threads and executing them in a multithreaded environment is an expensive process, primarily due to the overhead involved in context switches between threads.

Java multithreading and concurrency best practices

Albeit the fact that the Java Programming Language provides excellent support for working with threads, it is imperative that you are extra careful when working with multithreading in enterprise applications to deal with concurrency and performance issues to design and implement applications that are high performant, secure and scalable. In this section, we will explore the best practices that are involved when working with multithreading in Java.

Immutable types

It is advisable to use immutable classes when working in Java. Examples of immutable classes are String, Integer, etc. In using immutable classes and types, you need not worry about concurrency issues. The reason is that instance of immutable classes once created cannot be changed. When you modify a String object, a new String object is created.

Use locking judiciously

You should write minimal code inside lock - just lock the critical sections only. That way the application performance would be much better. It is preferable to take advantage of the lock over synchronized keyword.

Local variables

It is a good practice to use local variables rather than using class or instance variables. You might wonder that creating a local variable every time for each method you write is a pain and you might think of reusing class level variables. However, reusability here would come at the cost of performance and concurrency problems. In using local variables more in your code, each thread that accesses the method would have their own copy of objects – this would reduce the concurrency issues that you would otherwise have to handle had those variables been shared or in a class scope.

Avoid using static objects

The static keyword in Java is used to create static method, class and objects. A static member in a class belongs to the class itself rather than the instances of the class. However, static objects create concurrency issues. Hence, it is advisable to avoid using static objects or static variables in your code unless they are absolutely needed. If you are using static variables, you can make them static final constants instead. Consider using read-only collections for thread safety.

Concurrent collections

You should take advantage of concurrent collections over synchronized collections. Concurrent collections are built using the locking mechanism of the lock interface and so they can leverage the native concurrency mechanisms of the Java Virtual Machine much better.



In this article, we have had a glimpse at the basics of threads and multithreading and the best practices that should be adopted when working with multithreading in enterprise applications. Happy reading!



   
Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.Net, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He has more than 18 years of industry experience in IT, with more than 14 years in Microsoft .Net and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of eight books and more than 300 articles. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb’s Conference 2014 in Bangalore. He is also a regular speaker at SSWUG Virtual Conference. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date