Multithreading and concurrent library application 7 - data sharing between threads 2

Keywords: github git

In the last two articles, we talked about the way of data sharing. This article focuses on one small topic to consolidate it
Suppose there are four threads, two to add 1 to j and two to subtract 1 from J
Two ways of thread data sharing
1. Encapsulate the shared data in another object, and then pass this object to each Runable object one by one. Each thread's operation on shared data is also assigned to that object. In this way, it is easy to realize the mutual exclusion and communication of various operations for the data.

public class ThreadShareData {
    public static void  main(String[] args ){
        ShareData1 sd1=new ShareData1();
        new Thread(new Runnable1(sd1)).start();
        new Thread(new Runnable1(sd1)).start();
        new Thread(new Runnable2(sd1)).start();
        new Thread(new Runnable2(sd1)).start();
    }
}
class Runnable1 implements  Runnable{
    private ShareData1 sd1;

    public Runnable1(ShareData1 sd1){
        this.sd1=sd1;
    }
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                sd1.decrement();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Runnable2 implements  Runnable{
    private ShareData1 sd1;

    public Runnable2(ShareData1 sd1){
        this.sd1=sd1;
    }
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                sd1.increment();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
class ShareData1{
    private int j=0;
    public void increment(){
        System.out.print(j+" ");
        j++;
        System.out.println(" add after "+j);
    }
    public void decrement(){
        System.out.print(j+" ");
        j--;
        System.out.println(" decre after "+j);
    }

}

2. Take each runnable object as an internal class object as an external member variable

 public class ThreadShareData2 {
    public static void  main(String[] args ){

        final ShareData1 sd1=new ShareData1();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.increment();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.decrement();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.increment();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.decrement();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();

    }
}

class ShareData1 {
    private int j=0;
    public synchronized void increment(){
        System.out.print(j+" ");
        j++;
        System.out.println(" add after "+j);
    }
    public synchronized  void decrement(){
        System.out.print(j+" ");
        j--;
        System.out.println(" decre after "+j);
    }

    public void run() {

    }
}

Article address: http://www.haha174.top/article/details/253024
Project source code: https://github.com/haha174/thread-learning.git

Posted by dookie on Sun, 03 May 2020 09:19:54 -0700