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();
    		
    	}
    
    }
    
  • 相关阅读:
    学习完java基础,总结的一些东西,希望大佬们茶余饭后看看,如有错误还请指正
    JDK和JRE的简单概述
    堆排序
    mybatis的一级缓存
    重用执行器和批处理执行器
    JDBC statement和mybatis mapper statement
    深入浅出Mybatis技术原理与实战(杨开振)(带详细书签) PDF 下载 高清 完整版+源码
    mybatis执行器1
    mybatis执行器1---简单描述JDBC
    JDBC不再需要Class.forName()来显式加载jdbc驱动
  • 原文地址:https://www.cnblogs.com/qwop/p/6637242.html
Copyright © 2011-2022 走看看