Java TheadLocal multithreading resource usage
Thread processing resources:
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;} @ Overridepublic void run () {super. run (); RegisterManager. getRegisterManager (). startRegister (map); if (RegisterManager. getRegisterManager (). getRegister ()! = Null) {System. out. println ("Haha, yes") ;}} public void print () {if (RegisterManager. getRegisterManager (). getRegister () = null) {System. out. println ("no ");}}}
Multi-threaded Resource Management:
package org.shefron.fc.thread.threadlocal;import java.util.HashMap;public class RegisterManager {private ThreadLocal
localRegister = new ThreadLocal
();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();}}
Main method test class:
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 ()); // sub-thread handle. start (); if (handle! = Null) {// This method is located in the main thread handle. print () ;}} System. out. println (RegisterManager. getRegisterManager (). getRegister ());}}
Java TheadLocal multithreading resource usage.