ThreadLocal代码逻辑

本例子主要目的,给每个线程赋予单独的变量副本,使各线程之间不互相影响

/**
 * @Package: com.example.thread
 * @Description: 序列号生成器
 * @author: liuxin
 * @date: 17/3/31 下午1:50
 */
public interface Sequence {
    int getNumber();
}

/**
 * @Package: com.example.thread
 * @Description: 每个线程调用都会刷新值去覆盖,所以,所有线程共同消费number
 * @author: liuxin
 * @date: 17/3/31 下午1:51
 */
public class SequenceA implements Sequence {
    private static volatile  int number=0;
    @Override
    public int getNumber() {
        number=number+1;
        return number;
    }
}

/**
 * @Package: com.example.thread
 * @Description: 局部线程变量池,每个线程使用自己的,所以各个线程之间不共享
 * @author: liuxin
 * @date: 17/3/31 下午1:58
 */
public class SequenceB implements Sequence {

    /**
     * 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,
     * 所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。
     * 从线程的角度看,目标变量就象是线程的本地变量,这也是类名中“Local”所要表达的意思。
     */
    private static ThreadLocal numberContainer = new ThreadLocal() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    @Override
    public int getNumber() {
        numberContainer.set(numberContainer.get() + 1);
        return numberContainer.get();
    }
}
/**
 * @Package: com.example.thread
 * @Description: 多线程
 * @author: liuxin
 * @date: 17/3/23 下午4:35
 */
public class MyThread extends Thread {
    Sequence sequence;

    MyThread(Sequence sequence) {
        this.sequence = sequence;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName() + "<---->" + sequence.getNumber());
        }
    }

    public static void main(String[] args) {

        Sequence sequence = new SequenceA();
        MyThread t0 = new MyThread(sequence);
        MyThread t1 = new MyThread(sequence);
        MyThread t2 = new MyThread(sequence);
        t0.start();t0.setPriority(10);
        t1.start();t1.setPriority(9);
        t2.start();t2.setPriority(8);


        Sequence sequence1 = new SequenceB();
        MyThread t3 = new MyThread(sequence1);
        MyThread t4 = new MyThread(sequence1);
        MyThread t5 = new MyThread(sequence1);
        t3.start();t3.setPriority(7);
        t4.start();t4.setPriority(6);
        t5.start();t5.setPriority(5);


    }
}
        Thread-0<---->1
        Thread-0<---->2
        Thread-0<---->3
        Thread-1<---->4
        Thread-1<---->5
        Thread-1<---->6
        Thread-2<---->7
        Thread-2<---->8
        Thread-2<---->9
        Thread-3<---->1
        Thread-4<---->1
        Thread-3<---->2
        Thread-5<---->1
        Thread-3<---->3
        Thread-4<---->2
        Thread-5<---->2
        Thread-5<---->3
        Thread-4<---->3

自定义一个ThreadLocal,设计思路就是为每个线程保存一个变量副本
使用Map实现

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package: com.example.thread
 * @Description: 手动实现一个TheadLocal
 * @author: liuxin
 * @date: 17/3/31 下午2:22
 */
public class MyTheadLocal {
    private Map container = Collections.synchronizedMap(new HashMap());
    
    public void set(T value){
        container.put(Thread.currentThread(),value);
    }
    
    public T get(){
       return container.get(Thread.currentThread());
    }
    
    public void remove(){
        container.remove(Thread.currentThread());
    }
    
    protected T initialValue(){
        return null;
    }
}

你可能感兴趣的:(ThreadLocal代码逻辑)