mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix weak bug
This commit is contained in:
@@ -11,6 +11,17 @@ import java.io.Serializable;
|
||||
public class MutableObj<T> implements Mutable<T>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 构建MutableObj
|
||||
* @param value 被包装的值
|
||||
* @param <T> 值类型
|
||||
* @return MutableObj
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <T> MutableObj<T> of(T value){
|
||||
return new MutableObj<>(value);
|
||||
}
|
||||
|
||||
private T value;
|
||||
|
||||
/**
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.mutable.MutableObj;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.ref.PhantomReference;
|
||||
@@ -31,4 +34,23 @@ public class ReferenceUtilTest {
|
||||
// get方法永远都返回null,PhantomReference只能用来监控对象的GC状况
|
||||
Assert.assertNull(integerReference.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void gcTest(){
|
||||
// https://blog.csdn.net/zmx729618/article/details/54093532
|
||||
// 弱引用的对象必须使用可变对象,不能使用常量对象(比如String)
|
||||
WeakReference<MutableObj<String>> reference = new WeakReference<>(new MutableObj<>("abc"));
|
||||
int i=0;
|
||||
while(true){
|
||||
if(reference.get()!=null){
|
||||
i++;
|
||||
Console.log("Object is alive for {} loops - ", i);
|
||||
System.gc();
|
||||
}else{
|
||||
Console.log("Object has been collected.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user