• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

guidelines to thread-safe code

How to write thread safe code?

May 24, 2017 By j2eereference Leave a Comment

It is very important to write thread safe code if application is multithreaded to avoid deadlock and race conditions. Below listed points should be followed in multithreading environment.

1. Use synchronized block or synchronized methods if code is accessed by multiple threads to avoid race conditions. Only one thread can enter synchronized method or block by acquiring lock on the object at a time and hence avoid race condition.

2. Use volatile variables When there is an independent value to be shared among mutiple threads like counter and flag. Volatile variables read values from main memory and not from cache and in this way get updated value of variables.This also avoid race condition.

3. As we know, every thread has its own stack which contains local variables and value of local variables are confined to that thread stack and not be shared with other threads running in the same process. So, local variables are thread safe and if possible try to use local variables.

4. Use Atomic Classes like AtomicInteger as atomic operations are thread safe.

5. Final variables are also thread safe as once we assign any value or reference of an object then it cannot change value of variable or point to other object reference later in the code.

We declare and assign final variable of integer type with value 10 before calling executeMethod. Inside executeMethod we are trying to change the value of i to 0 then it will give compilation error.

public class FinalDemo {
final int i=10;
void executeMethod(){
i=0; //compilation error
}
}

6. If you are building multithreading application then use thread-safe collection classes for code safety like use ConcurrentHashMap in place of HashMap.

7. Use ThreadLocal Class as this class provides thread-safe local variables.

8. Use Immutable Objects like String rather than StringBuffer which is mutable . Immutable Objects are thread safe as their state cannot be changed once created and any state change will produce new object.

9. Use VisualVM or jstack to detect deadlock problems and time taken by threads for completing its task.

Related Posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof

Filed Under: Core Java Tagged With: guidelines to thread-safe code, How to write thread safe code

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.