• 试试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();
    		
    	}
    
    }
    
  • 相关阅读:
    C++读写文件并排序
    我的vim配置---jeffy-vim-v2.2.tar
    vim 代码注释插件
    我的vim配置---jeffy-vim-v2.1.tar
    linux中screen命令的用法
    Install and Enable Telnet server in Ubuntu Linux
    Telnet环境变量
    Telnet窗口尺寸选项
    TELNET终端类型选项
    Telnet技术白皮书
  • 原文地址:https://www.cnblogs.com/qwop/p/6637242.html
Copyright © 2020-2023  润新知