标签:使用 java ar for art sp on c new
线程处理资源类:
package org.shefron.fc.thread.threadlocal; import java.util.HashMap; public class HandleThead extends Thread { private HashMap map = null; public HandleThead(HashMap hashMap) { map = hashMap; } @Override public void run() { super.run(); RegisterManager.getRegisterManager().startRegister(map); if (RegisterManager.getRegisterManager().getRegister() != null){ System.out.println("呵呵,有啊"); } } public void print(){ if (RegisterManager.getRegisterManager().getRegister() == null){ System.out.println("没有了"); } } }
多线程资源管理类:
package org.shefron.fc.thread.threadlocal; import java.util.HashMap; public class RegisterManager { private ThreadLocal<HashMap> localRegister = new ThreadLocal<HashMap>(); private static RegisterManager manager = new RegisterManager(); private RegisterManager(){ } public static RegisterManager getRegisterManager(){ return manager; } public void startRegister(HashMap infoMap){ this.localRegister.set(infoMap); } public HashMap getRegister(){ return this.localRegister.get(); } }
主方法测试类:
package org.shefron.fc.thread.threadlocal; import java.util.HashMap; public class TestThreadLocal { /** * @param args */ public static void main(String[] args) { HashMap map = new HashMap(); map.put("hehe", "haha"); RegisterManager.getRegisterManager().startRegister(map); HandleThead handle = null; for (int i=1;i<4;i++){ handle = new HandleThead(new HashMap()); //子线程 handle.start(); if(handle != null){ //此方法位于主线程 handle.print(); } } System.out.println(RegisterManager.getRegisterManager().getRegister()); } }
标签:使用 java ar for art sp on c new
原文地址:http://blog.csdn.net/shefron/article/details/39056773