2022-12-28-面试题整理

1. Spring中Bean创建完成后执行指定代码的几种实现方式

  1. 实现ApplicationListener接口
    实现ApplicationListener接口并实现方法onApplicationEvent()方法,Bean在创建完成后会执行onApplicationEvent()方法
@Component
public class DoByApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    public DoByApplicationListener() {
        System.out.println("DoByApplicationListener constructor");
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (Objects.isNull(event.getApplicationContext().getParent())) {
            System.out.println("DoByApplicationListener do something");
        }
    }
}
  1. 实现InitializingBean接口
    实现InitializingBean接口并实现方法afterPropertiesSet(),Bean在创建完成后会执行afterPropertiesSet()方法
@Component
public class DoByInitializingBean implements InitializingBean {

    public DoByInitializingBean() {
        System.out.println("DoByInitializingBean constructor");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitByInitializingBean do something");
    }
}
  1. 使用@PostConstruct注解
    在Bean的某个方法上使用@PostConstruct注解,Bean在创建完成后会执行该方法
@Component
public class DoByPostConstructAnnotation {

    public DoByPostConstructAnnotation() {
        System.out.println("DoByPostConstructAnnotation constructor");
    }

    @PostConstruct
    public void init() {
        System.out.println("InitByPostConstructAnnotation do something");
    }
}

2. ThreadLocal的优缺点?

答案
  1. ThreadLocal提供线程局部变量;开箱即用开销小,可以代替多线程访问共享变量时需要上锁的需要;
  2. 线程资源一致:基于线程池模型去synchronized很危险:排队去锁消耗的时间导致CPU用不完。
    例如:线程池中24个线程,打来100个并发量,一次只能24个,而用ThreadLocal收集数据很快且安全。同一个事务下的任务们必须保证原子性,也就是说下图中的task们都须通过同一个线程的getConnection()方法拿到完全相同的数据。首先判断ThreadLocalMap中是否存在锁需值,如果没有就去连接池取,取到的值先存到线程共享的ThreadLocalMap中,再返回给对应task。
    在这里插入图片描述

题外知识

ThreadLocal原理
1. 结构模型

系统里经常使用ThreadLocal保存线程上下文信息,是一个创造线程局部变量的类,适用于线程独享且在类和方法间共享的场景。ThreadLocal提供了线程本地的实例。它与普通变量的区别在于,每个使用该变量的线程都会初始化一个完全独立的实例副本。ThreadLocal变量通常被private static修饰。当一个线程结束时,它所使用的所有ThreadLocal相对的实例副本都可被回收。
在这里插入图片描述

2. 实现原理

在各种多线程语言都有,Java用哈希表实现(ThreadLocalMap)
在这里插入图片描述

3.源码
  • 1.构造函数ThreadLocal()
static class ThreadLocalMap {

        /**
         * The entries in this hash map extend WeakReference, using
         * its main ref field as the key (which is always a
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
         * == null) mean that the key is no longer referenced, so the
         * entry can be expunged from table.  Such entries are referred to
         * as "stale entries" in the code that follows.
         */
        static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;

            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }
    1. 初始化 initialValue()
      只有线程在第一次调用get()方法时,执行此方法。public T get() 返回ThreadLocal中当前线程副本的值。变量第一次get时没有当前线程的值,就调用initialValue()的返回值
    1. 访问器 get/set
    1. 回收 remove
      以下手动来个demo(涉及到延迟加载)
public class ThreadLocalDemo {
    static ThreadLocal<Long> x = ThreadLocal.withInitial(() -> {
        System.out.println("Initail Value run...");
        return Thread.currentThread().getId();  // 拿到当前线程ID
    });

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println("main new Thread线程名:" + Thread.currentThread().getName() + ":" + x.get());
        }).start();
        System.out.println("main 线程名:" + Thread.currentThread().getName() + ":" + x.get());
    }
}

结果是:(main函数中两个不同get都各调用了一次initialValue方法,每个线程都有自己的变量值)

Initail Value run...
Initail Value run...
main new Thread线程名:Thread-0:12
main 线程名:main:1

下面是测试set方法:

public class APITests {
    public static ThreadLocal<Long> threadLocal = ThreadLocal.withInitial(() -> {
        System.out.println("Initail Value run...");
        return Thread.currentThread().getId();
    });

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println("main new Thread " + Thread.currentThread().getName() + ":" + threadLocal.get());
        }).start();
        threadLocal.set(6L);
        System.out.println("main " + Thread.currentThread().getName() + ":" + threadLocal.get());
    }
}

得到的结果如下:可见initialValue只调用了一次

main main:6
Initail Value run...
main new Thread Thread-0:12
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值