`
includemain
  • 浏览: 32137 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

试试ThreadLocal

    博客分类:
  • Java
阅读更多
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();
		
	}

}
分享到:
评论
10 楼 beneo 2010-06-09  
zha_zi 写道
为什么会内存泄露?求解答



我目前觉得有2种内存泄漏

1. 线程池引起的内存泄漏

2. 如果你的Threadlocal里面的value,强引用了你的ThreadLocal自身
9 楼 yunj 2010-06-09  
真巧今天就看了别人分享的ThreadLocal,有些新发现:
原以为ThreadLocal里有一个Map, Key为Thread.id或Thread对象
今天知道, 这个Map在Thread类里,key 为ThreadLocal对象
8 楼 J-catTeam 2010-05-27  
ThreadLocal 和线程安全没有什么关系
7 楼 laolinshi 2010-05-27  
你的Resource是单实例的,放到ThreadLocal也不能保证线程安全,这样做有意义吗?不明白你这样做的理由。
6 楼 zha_zi 2010-05-27  
为什么会内存泄露?求解答
5 楼 qiren83 2010-05-27  
我咋看不懂呀
4 楼 qiren83 2010-05-27  
The resource starting...!
The resource ending...!
The resource destroying...!
Thread[Thread-2,5,main] : go away!
The resource starting...!
The resource ending...!
The resource destroying...!
Thread[Thread-0,5,main] : go away!
The resource starting...!
The resource ending...!
The resource destroying...!
Thread[Thread-1,5,main] : 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[Thread-6,5,main] : go away!
The resource starting...!
The resource ending...!
The resource destroying...!
Thread[Thread-5,5,main] : go away!
The resource starting...!
The resource ending...!
The resource destroying...!
Thread[Thread-3,5,main] : go away!
Thread[Thread-4,5,main] : go away!
Thread[Thread-7,5,main] : go away!


有啥不对吗?
3 楼 xx521 2010-05-27  
有道理:







久久资讯网
2 楼 beneo 2010-05-26  
sdh5724 写道
万恶之源, 内存泄露主因。


如果没有线程池,就没有内存泄漏了吧,对与ThreadLocal
1 楼 sdh5724 2010-05-26  
万恶之源, 内存泄露主因。

相关推荐

Global site tag (gtag.js) - Google Analytics