Immutable Object mode - multi-threaded

Keywords: Programming

Immutable Object mode - multi-threaded

Preface

In multi-threaded programming, we often encounter the problem of modifying the value of an object. If there is no lock, the value will be inconsistent. Is there a way to ensure data consistency without locking? Of course, the Invariant Object Pattern introduced today can be achievedSuch an effect

problem

How to print 0-99 values without repeating them in a multi-threaded situation

Variable Object Mode
  1. First create a new Count object with the following code:
/**
 * @Author: Wang Chong 
 * @Date: 2019/9/2 22:02
 * @Version: V1.0
 */
public class Count {

    private int i;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}
  1. Create a new CountThread thread
/**
 * @Author: Wang Chong 
 * @Date: 2019/9/2 22:14
 * @Version: V1.0
 */
public class CountThread implements Runnable {
    private Count count;

    public CountThread(Count count) {
        this.count = count;
    }

    @Override
    public void run() {
        int i = count.getI();
        System.out.println("no immutable object,current is is :" + i);
    }
}
  1. Run the program
/**
 * @Author: Wang Chong 
 * @Date: 2019/9/2 22:17
 * @Version: V1.0
 */
public class CountMain {
    public static void main(String[] args) {
        Count count = new Count(0);
        for (int i=0; i<100;i++){
            count.setI(i);
            CountThread countThread = new CountThread(count);
            new Thread(countThread).start();
        }
    }
}
  1. The results are as follows:
.....Previous result omitted

no immutable object,current is is :93
no immutable object,current is is :93
no immutable object,current is is :94
no immutable object,current is is :96
no immutable object,current is is :85
no immutable object,current is is :97
no immutable object,current is is :98
no immutable object,current is is :91
no immutable object,current is is :99
no immutable object,current is is :99
no immutable object,current is is :99

You can find many duplicate numbers in the results, such as 93, 99, etc.

Invariant Object Mode
  1. First create a new immutable CountImmutable object with the following code:
/**
 * @Author: Wang Chong
 * @Date: 2019/9/2 22:02
 * @Version: V1.0
 */
public final class CountImmutable {

    private volatile int i;

    CountImmutable(int i){
      this.i = i;
    }

    public int getI() {
        return i;
    }

    /**
     * Return a new instance
     * @param i
     * @return
     */
    public CountImmutable set(int i){
        return new CountImmutable(i);
    }

}
  1. Create a new CountImmutableThread thread
/**
 * @Author: Wang Chong 
 * @Date: 2019/9/2 22:14
 * @Version: V1.0
 */
public class CountImmutableThread implements Runnable {
    private CountImmutable count;

    public CountImmutableThread(CountImmutable count) {
        this.count = count;
    }

    @Override
    public void run() {
        int i = count.getI();
        System.out.println("current is :" + i);
    }
}
  1. Run the program
/**
 * @Author: Wang Chong 
 * @Date: 2019/9/2 22:17
 * @Version: V1.0
 */
public class ImmutableDemo {
    public static void main(String[] args) {
        CountImmutable count = new CountImmutable(0);
        for (int i=0; i<100;i++){
            count = count.set(i);
            CountImmutableThread countThread = new CountImmutableThread(count);
            new Thread(countThread).start();
        }
    }
}
  1. The results are as follows:
.....Previous result omitted

current is  :69
current is  :45
current is  :89
current is  :54
current is  :57
current is  :58
current is  :39
current is  :47
current is  :51
current is  :55
current is  :91
current is  :95
current is  :99

From the code above, you can see that in immutable object mode, numerical consistency can also be guaranteed without locking in a multithreaded environment

summary

The immutable object mode applies in the following scenarios:

  • Modeled object state changes infrequently
  • Write a set of related data at the same time to ensure its atomicity
  • When an object's key is to be used as the key of a secure HashMap

Posted by mattkenefick on Mon, 02 Sep 2019 19:18:02 -0700