论坛首页 Java企业应用论坛

试试ThreadLocal

浏览 5266 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (10)
作者 正文
   发表时间:2010-05-25   最后修改:2010-05-31
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();
		
	}

}
   发表时间:2010-05-26  
万恶之源, 内存泄露主因。
0 请登录后投票
   发表时间:2010-05-26  
sdh5724 写道
万恶之源, 内存泄露主因。


如果没有线程池,就没有内存泄漏了吧,对与ThreadLocal
0 请登录后投票
   发表时间: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!


有啥不对吗?
0 请登录后投票
   发表时间:2010-05-27  
我咋看不懂呀
0 请登录后投票
   发表时间:2010-05-27  
为什么会内存泄露?求解答
0 请登录后投票
   发表时间:2010-05-27   最后修改:2010-05-27
你的Resource是单实例的,放到ThreadLocal也不能保证线程安全,这样做有意义吗?不明白你这样做的理由。
0 请登录后投票
   发表时间:2010-05-27  
ThreadLocal 和线程安全没有什么关系
0 请登录后投票
   发表时间:2010-06-09  
真巧今天就看了别人分享的ThreadLocal,有些新发现:
原以为ThreadLocal里有一个Map, Key为Thread.id或Thread对象
今天知道, 这个Map在Thread类里,key 为ThreadLocal对象
0 请登录后投票
   发表时间:2010-06-09  
zha_zi 写道
为什么会内存泄露?求解答



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

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

2. 如果你的Threadlocal里面的value,强引用了你的ThreadLocal自身
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics