ThreadLocal 实战

x33g5p2x  于2022-04-22 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(304)

一 简单例子

1 代码

package concurrent.threadlocal;

/**
* ThreadLocal测试
*
* @author cakin
*/
public class ThreadLocalSimpleTest {
    private static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "Alex");
    /**
     * JVM start main thread
     *
     * @param args 启动参数
     */
    public static void main(String[] args) throws InterruptedException {
        threadLocal.set("Alex2");
        Thread.sleep(1000);
        String value = threadLocal.get();
        System.out.println(value);
    }
}

2 测试

Alex2

二 复杂例子

1 代码

package concurrent.threadlocal;

import java.util.Random;

/**
* ThreadLocal测试
*
* @author cakin
*/
public class ThreadLocalComplexTest {
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();
    private static final Random RANDOM = new Random(System.currentTimeMillis());

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            threadLocal.set("Thread-T1");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T1");
        Thread t2 = new Thread(() -> {
            threadLocal.set("Thread-T2");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
    }
}

2 测试

Thread-T2:Thread-T2

Thread-T1:Thread-T1

main:null

三 模拟 ThreadLocal

1 模拟ThreadLocal

package concurrent.threadlocal;

import java.util.Random;

/**
* 模拟 ThreadLocal
*
* @author cakin
*/
public class ThreadLocalSimulatorTest {
    private static final ThreadLocalSimulator<String> threadLocal = new ThreadLocalSimulator<String>() {
        @Override
        public String initialValue() {
            return "No Value";
        }
    };

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            threadLocal.set("Thread-T1");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T1");
        Thread t2 = new Thread(() -> {
            threadLocal.set("Thread-T2");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
    }
}

2 测试代码

package concurrent.threadlocal;

import java.util.Random;

/**
* 模拟 ThreadLocal
*
* @author cakin
*/
public class ThreadLocalSimulatorTest {
    private static final ThreadLocalSimulator<String> threadLocal = new ThreadLocalSimulator<String>() {
        @Override
        public String initialValue() {
            return "No Value";
        }
    };

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            threadLocal.set("Thread-T1");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T1");
        Thread t2 = new Thread(() -> {
            threadLocal.set("Thread-T2");
            try {
                Thread.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }, "Thread-T2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
    }
}

3 测试

Thread-T2:Thread-T2

Thread-T1:Thread-T1

main:No Value

相关文章