Home Software Development Thread Security in Java | Developer.com

Thread Security in Java | Developer.com

0
Thread Security in Java | Developer.com

[ad_1]

Developer.com content material and product suggestions are editorially impartial. We might generate income while you click on on hyperlinks to our companions. Be taught Extra.

In our “What’s Concurrency in Java?” tutorial, we realized that issues can happen inside a multi-threaded setting if a number of threads attempt to entry and alter the identical knowledge on the identical time. It is a significant issue, as it will possibly result in execution deadlocks and knowledge corruption. A program that permits a number of threads to vary the identical knowledge construction or objects concurrently is known as being not thread-safe. Conversely, a program that’s thread secure prevents different threads from engaged on the identical object when a thread is already working with it. On this programming tutorial, we study 4 comparatively simple methods of reaching thread security in our Java applications.

For those who missed it, we suggest studying our earlier a part of this collection on Java threading: What’s Concurrency in Java?

Leap to:

Utilizing Synchronization in Java

The primary method to make a program thread secure is to make use of Synchronization. Merely put, Synchronization is the method of permitting just one thread at a time to finish a selected operation. It resolves the inconsistency drawback by stopping a number of threads from accessing the identical useful resource on the identical time. Synchronization makes use of a synchronized key phrase, which is a particular modifier that creates a block of code often called a important part.

Right here is an instance Java program that increments a price by 5 on two separate threads:

package deal com.developer;

public class Maths {
  void add5(int num) {
    // Create a thread occasion
    Thread t = Thread.currentThread();
    for (int i = 1; i <= 5; i++) {
      System.out.println(t.getName() + " : " + (num + i));
    }
  }
}


package deal com.developer;

public class Maths2 extends Thread {
  Maths maths = new Maths();
  public void run() {
    maths.add5(10);
  }
}


package deal com.developer;

public class SynchronizationDemo {

  public static void principal(String[] args) {
    Maths2 maths = new Maths2();
    
    Thread t1 = new Thread(maths);
    Thread t2 = new Thread(maths);
    
    t1.setName("Thread 1");
    t2.setName("Thread 2");
    
    t1.begin();
    t2.begin();
  }
}

As anticipated, the incremented worth jumps everywhere as every thread accesses the identical variable concurrently:

Thread 1 : 11
Thread 1 : 12
Thread 1 : 13
Thread 2 : 11
Thread 1 : 14
Thread 2 : 12
Thread 2 : 13
Thread 1 : 15
Thread 2 : 14
Thread 2 : 15

Including the synchronized key phrase to the add5() methodology resolves this concern:

public class Maths {
  synchronized void add5(int num) {
    // Create a thread occasion
    Thread t = Thread.currentThread();
    for (int i = 1; i <= 5; i++) {
      System.out.println(t.getName() + " : " + (num + i));
    }
  }
}

Now every thread completes its work in flip:

Thread 1 : 11
Thread 1 : 12
Thread 1 : 13
Thread 1 : 14
Thread 1 : 15
Thread 2 : 11
Thread 2 : 12
Thread 2 : 13
Thread 2 : 14
Thread 2 : 15

Learn: Greatest Instruments for Java Cellular Improvement

Utilizing the risky Key phrase in Java

One other method to obtain thread security in Java is to make use of the risky key phrase. It’s a area modifier that ensures that the article can be utilized by a number of threads on the identical time with out inflicting the problematic behaviors talked about above.

The next instance code declares and instantiates two integers utilizing the risky key phrase:

package deal com.developer;

public class VolatileKeywordDemo {
  static risky int int1 = 0, int2 = 0;
  
  static void methodOne() {
    int1++;
    int2++;
  }
  
  static void methodTwo() {
    System.out.println("int1=" + int1 + " int2=" + int2);
  }
  
  public static void principal(String[] args) {
  
    Thread t1 = new Thread() {
      public void run() {
        for (int i = 0; i < 5; i++) {
          methodOne();
        }
      }
    };
    
    Thread t2 = new Thread() {
      public void run() {
        for (int i = 0; i < 5; i++) {
          methodTwo();
        }
      }
    };
    
    t1.begin();
    t2.begin();
  }
}

As we will observe in this system output, each variables have been totally incremented by the primary thread earlier than the second thread outputs their values:

int1=5 int2=5
int1=5 int2=5
int1=5 int2=5
int1=5 int2=5
int1=5 int2=5

Easy methods to Use Atomic Variables in Java

One other method to obtain thread security in Java is to make use of atomic variables. Because the title suggests, atomic variables permit builders to carry out an atomic operation on a variable. Atomic variables decrease synchronization and assist keep away from reminiscence consistency errors. Essentially the most generally used atomic variables are AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference. Right here is a few instance Java code that makes use of AtomicInteger to increment two separate counters earlier than outputting their mixed worth:

package deal com.developer;

import java.util.concurrent.atomic.AtomicInteger;

public class Counter {
  AtomicInteger counter = new AtomicInteger();
  
  public void increment() {
    counter.incrementAndGet();
  }
}


package deal com.developer;

public class AtomicIntegerDemo {
  public static void principal(String[] args) throws Exception {
  
    Counter c = new Counter();
    
    Thread t1 = new Thread(new Runnable() {
      public void run() {
        for (int i = 1; i <= 5000; i++) {
          c.increment();
        }
      }
    });
    
    Thread t2 = new Thread(new Runnable() {
        public void run() {
          for (int i = 1; i <= 5000; i++) {
            c.increment();
          }
        }
    });
    
    t1.begin();
    t2.begin();
    
    t1.be part of();
    t2.be part of();
    
    System.out.println(c.counter);
  }
}

Working the above program produces an output of “10000“.

Learn: High Java Frameworks

Easy methods to Use the ultimate Key phrase in Java

Ultimate Variables are at all times thread secure in Java as a result of, as soon as assigned, a reference to an object can not level to a different object. Here’s a brief program to show find out how to use the closing key phrase in Java:

package deal com.developer;

public class FinalKeywordDemo {
  closing String aString = new String("Immutable");
  
  void someMethod() {
    aString = "new worth";
  }
}

Built-in growth environments (IDEs) is not going to even allow you to run the above code and can present a compiler error concerning the try to reassign a price to the ultimate aString variable:

Java final Keyword example

Ultimate Ideas on Thread Security in Java

This programming tutorial introduced 4 methods of reaching thread security in our Java applications, particularly: utilizing Synchronization, the risky Key phrase, through atomic variables, and the closing key phrase. There are different methods to attain thread security in Java, however these require barely extra effort on the a part of the developer. These embrace the usage of locks from the java.util.concurrent.locks package deal and utilizing thread secure assortment lessons corresponding to ConcurrentHashMap.

Subsequent Steps

Now that you’ve a agency understanding of among the methods to attain thread security in Java, we suggest testing just a few of our different tutorials on threading and multithreading in Java:

[ad_2]