天天看点

代码量提升之路 - 线程范围内共享变量

线程范围内的数据共享 通过两个模块取得

package com;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ThreadScopeShareData {
    private static int data = 0;
    public static Map<Thread, Integer> threadMap = new HashMap<Thread, Integer>();
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (threadMap) {
                        int data = new Random().nextInt();
                        System.out.println(Thread.currentThread().getName()
                                + " has put data:" + data);
                        threadMap.put(Thread.currentThread(), data);
                        new BusinessA().getDat();
                        new BuninessB().getDat();
                    }                
                }
            }).start();
        }
    }
    static class BusinessA {
        public void getDat() {
            data = threadMap.get(Thread.currentThread());
            System.out.println("BusinessA from "
                    + Thread.currentThread().getName() + " get data:" + data);
        }
    }
    static class BuninessB {
        public void getDat() {
            data = threadMap.get(Thread.currentThread());
            System.out.println("BuninessB from "
                    + Thread.currentThread().getName() + " get data:" + data);
        }
    }
}      
package com;
import java.util.Random;
public class ThreadLocalTest {
    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread()
                            + " has put data: " + data);
                    threadLocal.set(data);
                    new BusinessA().getData();
                    new BusinessB().getData();
                }
            }).start();
        }
    }
    static class BusinessA {
        public void getData() {
            int data = threadLocal.get();
            System.out.println("BusinessA from " + Thread.currentThread()
                    + " get data: " + data);
        }
    }
    static class BusinessB {
        public void getData() {
            int data = threadLocal.get();
            System.out.println("BusinessB from " + Thread.currentThread()
                    + " get data: " + data);
        }
    }
}      
package com;
import java.util.Random;
public class ThreadLocalPureTest {
    private static ThreadLocal<DataEntity> myThreadLocal = new ThreadLocal<DataEntity>();
    /*private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();*/
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread()
                            + " has put data: " + data);
                    /*threadLocal.set(data);*/
                    DataEntity dataEntity = new DataEntity();
                    dataEntity.setAge(data);
                    dataEntity.setName("name" + data);
                    myThreadLocal.set(dataEntity);
                    new BusinessA().getData();
                    new BusinessB().getData();
                }
            }).start();
        }
    }
    static class BusinessA {
        public void getData() {
            /*int data = threadLocal.get();
            System.out.println("BusinessA from " + Thread.currentThread()
                    + " get data: " + data);*/
            DataEntity dataEntity = myThreadLocal.get();
            System.out.println("BusinessA from " + Thread.currentThread()
                    + " get EntityData: " + dataEntity.getName() + ", " + dataEntity.getAge());
        }
    }
    static class BusinessB {
        public void getData() {
            /*int data = threadLocal.get();
            System.out.println("BusinessB from " + Thread.currentThread()
                    + " get data: " + data);*/
            DataEntity dataEntity = myThreadLocal.get();
            System.out.println("BusinessA from " + Thread.currentThread()
                    + " get EntityData: " + dataEntity.getName() + ", " + dataEntity.getAge());
        }
    }
}
class DataEntity {
    private String name;
    private int age;
         
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}      
package com;
import java.util.Random;
public class ThreadLocalPureTest {
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println(Thread.currentThread()
                            + " has put data: " + data);
                    DataEntity.getThreadInstance().setAge(data);
                    DataEntity.getThreadInstance().setName("name" + data);
                    new BusinessA().getData();
                    new BusinessB().getData();
                }
            }).start();
        }
    }
    static class BusinessA {
        public void getData() {
            DataEntity dataEntity = DataEntity.getThreadInstance();
            System.out.println("BusinessA from " + Thread.currentThread()
                    + " get EntityData: " + dataEntity.getName() + ", " + dataEntity.getAge());
        }
    }
    static class BusinessB {
        public void getData() {
            DataEntity dataEntity = DataEntity.getThreadInstance();
            System.out.println("BusinessB from " + Thread.currentThread()
                    + " get EntityData: " + dataEntity.getName() + ", " + dataEntity.getAge());
        }
    }
}
class DataEntity {
      
    private String name;
    private int age;
    private static ThreadLocal<DataEntity> mapLocal = new ThreadLocal<DataEntity>();
      
    private DataEntity(){}
      
    public static DataEntity getThreadInstance(){
        DataEntity instance = mapLocal.get();
        if (instance == null) {
            instance = new DataEntity();
            mapLocal.set(instance);
        }
          
        return instance;
    }
      
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}