zoukankan      html  css  js  c++  java
  • 试试ThreadLocal

    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();
    		
    	}
    
    }
    
  • 相关阅读:
    hdu4911 简单树状数组
    hdu4911 简单树状数组
    hdu4912 LCA+贪心
    hdu4912 LCA+贪心
    hdu4907 水dp 或者set
    hdu4907 水dp 或者set
    hdu4908 中位数子串
    hdu4908 中位数子串
    hdu4909 状态压缩(偶数字符子串)
    hdu4909 状态压缩(偶数字符子串)
  • 原文地址:https://www.cnblogs.com/qwop/p/6637242.html
Copyright © 2011-2022 走看看