public final class Resource {
private static Resource instance = new Resource();
private Resource() {
System.err.println("The resource initializing...!");
}
public static Resource getInstance() {
return instance;
}
public void start() {
System.err.println("The resource starting...!");
}
public void end() {
System.err.println("The resource ending...!");
}
public void destory() {
System.err.println("The resource destroying...!");
}
}
/**
* Use the thread locale
* @author Administrator
*/
public final class ResourceUtil {
private static ThreadLocal<Resource> resHandler = new ThreadLocal<Resource>();
public final static Resource getResource() {
Resource resource = resHandler.get();
if (resource == null) {
resource = Resource.getInstance();
resHandler.set(resource);
}
return resource;
}
public final static void work() {
Resource resource = getResource();
resource.start();
resource.end();
destroy();
}
public final static void destroy() {
Resource resource = resHandler.get();
if (resource != null) {
resource.destory();
resHandler.remove();
}
resource = null;
}
/**
* @param args
*/
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
new Thread() {
@Override
public void run() {
ResourceUtil.work();
System.err.println(Thread.currentThread() + " : go away!");
}
}.start();
}
}
我目前觉得有2种内存泄漏
线程池引起的内存泄漏
如果你的Threadlocal里面的value,强引用了你的ThreadLocal自身
真巧今天就看了别人分享的ThreadLocal,有些新发现: 原以为ThreadLocal里有一个Map, Key为Thread.id或Thread对象 今天知道, 这个Map在Thread类里,key 为ThreadLocal对象
ThreadLocal 和线程安全没有什么关系
你的Resource是单实例的,放到ThreadLocal也不能保证线程安全,这样做有意义吗?不明白你这样做的理由。
为什么会内存泄露?求解答
我咋看不懂呀
The resource starting...! The resource ending...! The resource destroying...! Thread : go away! The resource starting...! The resource ending...! The resource destroying...! Thread : go away! The resource starting...! The resource ending...! The resource destroying...! Thread : go away! The resource starting...! The resource ending...! The resource destroying...! The resource starting...! The resource ending...! The resource destroying...! The resource starting...! The resource ending...! The resource destroying...! Thread : go away! The resource starting...! The resource ending...! The resource destroying...! Thread : go away! The resource starting...! The resource ending...! The resource destroying...! Thread : go away! Thread : go away! Thread : go away!
有啥不对吗?
有道理:
久久资讯网
如果没有线程池,就没有内存泄漏了吧,对与ThreadLocal
万恶之源, 内存泄露主因。