ITeye博客

试试ThreadLocal

includemain · 2010-05-25 10:27 · Java · 编程语言 · Markdown


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)

  1. #10 楼 · beneo · 2010-06-09 12:37

    zha_zi writes:

    为什么会内存泄露?求解答

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

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

    2. 如果你的Threadlocal里面的value,强引用了你的ThreadLocal自身

  2. #9 楼 · yunj · 2010-06-09 00:59

    真巧今天就看了别人分享的ThreadLocal,有些新发现: 原以为ThreadLocal里有一个Map, Key为Thread.id或Thread对象 今天知道, 这个Map在Thread类里,key 为ThreadLocal对象

  3. #8 楼 · J-catTeam · 2010-05-27 15:00

    ThreadLocal 和线程安全没有什么关系

  4. #7 楼 · laolinshi · 2010-05-27 10:49

    你的Resource是单实例的,放到ThreadLocal也不能保证线程安全,这样做有意义吗?不明白你这样做的理由。

  5. #6 楼 · zha_zi · 2010-05-27 10:10

    为什么会内存泄露?求解答

  6. #5 楼 · qiren83 · 2010-05-27 09:34

    我咋看不懂呀

  7. #4 楼 · qiren83 · 2010-05-27 09:33

    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!

    有啥不对吗?

  8. #3 楼 · xx521 · 2010-05-27 01:26

    有道理:

    久久资讯网

  9. #2 楼 · beneo · 2010-05-26 18:23

    sdh5724 writes:

    万恶之源, 内存泄露主因。

    如果没有线程池,就没有内存泄漏了吧,对与ThreadLocal

  10. #1 楼 · sdh5724 · 2010-05-26 15:26

    万恶之源, 内存泄露主因。

评论功能维护中,当前仅展示历史评论。