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();
    		
    	}
    
    }
    
  • 相关阅读:
    css盒模型
    css构造块级元素
    后台数据能刷新,前台页面显示不刷新问题
    java的代理和动态代理简单测试
    基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建
    SSH框架中配置log4j的方法
    Spring声明式事务配置管理方法
    Hibernate关联关系配置(一对多、一对一和多对多)
    java递归和反向递归
    Java实现几种常见排序方法
  • 原文地址:https://www.cnblogs.com/qwop/p/6637242.html
Copyright © 2011-2022 走看看