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();