gpt4 book ai didi

ThreadLocal 实战

转载 作者:知者 更新时间:2024-03-13 01:47:31 30 4
gpt4 key购买 nike

一 简单例子

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com