java的引用

71 阅读1分钟

1、虛引用:供垃圾回收器回收进行处理

2、软引用:是软的,如果栈内存超过了本身,初始值是20MB,如果软引用10MB,再创建一个强引用,软引用的就会给消灭。 可用来做缓存。 SoftReferenece

3、强引用:就是普通的引用。

4、弱引用:WeakReference。 特点:垃圾回收器看到弱引用,看到就直接垃圾处理器清走

ThreadLodal

package com.ycz.study_06_07;


/**
 * @Description:
 * @Author: Alex
 * @Date 2022-06-07-14:25
 * @Version: V1.0
 **/
public class TreadLocalStudy {
    private static ThreadLocal<Person> t1 = new ThreadLocal<>();

    public static void main(String[] args) {

        new Thread(()->{
            try {
                Thread.sleep(1000);
                t1.set(new Person("abc"));
                System.out.println(t1.get());
                //t1.remove()不用的时候清理掉
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                Thread.sleep(2000);
               // t1.set(new Person("abc"));//
                System.out.println(t1.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


    }


}


class Person{

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public Person(String name){
        this.name= name;
    }
}

ThreadLocal的set

 public void set(T value) {
        Thread t = Thread.currentThread();//当前线程
        ThreadLocalMap map = getMap(t);//往自己线程的map中设对象
        if (map != null)
            map.set(this, value);//key就是ThreadLocal
        else
            createMap(t, value);
    }
 private void set(ThreadLocal<?> key, Object value) {

            // We don't use a fast path as with get() because it is at
            // least as common to use set() to create new entries as
            // it is to replace existing ones, in which case, a fast
            // path would fail more often than not.

            Entry[] tab = table;
            int len = tab.length;
            int i = key.threadLocalHashCode & (len-1);

            for (Entry e = tab[i];
                 e != null;
                 e = tab[i = nextIndex(i, len)]) {
                ThreadLocal<?> k = e.get();

                if (k == key) {
                    e.value = value;
                    return;
                }

                if (k == null) {
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }

            tab[i] = new Entry(key, value);//下面的代碼
            int sz = ++size;
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }
static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;
            //继承了弱引用
            Entry(ThreadLocal<?> k, Object v) {
                super(k);//是一个弱引用
                //不会造成内存泄漏
                //用完了就设为空,垃圾回收器就回收,因为是弱引用
                value = v;
            }
        }

结果: com.ycz.study_06_07.Person@686e97cd

null

一个线程的Collection是一样的,使用ThreadLocal包含起来,这样才能保证@Transactional能正常使用