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开源框架推荐(全)
    Java性能提示(全)
    国外程序员整理的 C++ 资源大全 (zt)
    技术杂记之:在阿里云centos7上部署JDK MYSQL TOMCAT
    技术杂记之:vi使用入门
    Java全栈程序员之09:IDEA+GitHub
    SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
    SpringCloud无废话入门04:Hystrix熔断器及监控
    SpringCloud无废话入门03:Feign声明式服务调用
    SpringCloud无废话入门02:Ribbon负载均衡
  • 原文地址:https://www.cnblogs.com/qwop/p/6637242.html
Copyright © 2011-2022 走看看