스 레 드 탱크 게 으 름 로드 초기 탐색 실현

3965 단어 JAVA 심층 분석
단일 클래스:
package com.ldy.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

public class LazyThreadPool extends ThreadPoolExecutor{
	private static final Logger logger = Logger.getLogger(LazyThreadPool.class);
	/**
	 *  Executors.newFixedThreadPool(corePoolSize, threadFactory);
	 * return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue(),
                                      threadFactory);
	 *          
	 * @param corePoolSize
	 */
	private LazyThreadPool(int corePoolSize) {
		super(corePoolSize, corePoolSize, 0L, TimeUnit.MILLISECONDS, 
				new LinkedBlockingQueue(), 
				new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = Executors.defaultThreadFactory().newThread(r);
				//t.setDaemon(true);//      ,       
				logger.info("    :"+t);
				return t;
			}
		});
		logger.info("           :LazyThreadPool");
	}

	private static class ExecutorServiceHolder {
		private static ExecutorService executorService = new LazyThreadPool(1);//          
	}

	public static ExecutorService getExecutorService() {
		return ExecutorServiceHolder.executorService;
	}
	@Override
	protected void beforeExecute(Thread t, Runnable r) {
		System.out.println(t);
		if(r instanceof AbstractTask){
			logger.info("      :"+((AbstractTask)r).taskName);
		}
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		if(r instanceof AbstractTask){
			logger.info("      :"+((AbstractTask)r).taskName);
		}
	}

	@Override
	protected void terminated() {
		logger.info("     ");
	}
	@Override
	public void execute(Runnable command) {
		// TODO Auto-generated method stub
		super.execute(this.wrapRunnable(command,clientTrace()));
	}
	@Override
	public Future> submit(Runnable task) {
		// TODO Auto-generated method stub
		return super.submit(this.wrapRunnable(task,clientTrace()));
	}
	private Exception clientTrace(){
		return new Exception("       。");
	}
	/**
	 * 1、          
	 * 2、             
	 * @param command
	 * @return
	 */
	private Runnable wrapRunnable(final Runnable command,final Exception clientTrace){
		return new Runnable() {
			@Override
			public void run() {
				try{
					command.run();
				}catch(Exception e){
					clientTrace.printStackTrace();//       
					e.printStackTrace();//             
					throw e;
				}
			}
		};
	}
	
}

작업 추상 화:
package com.ldy.utils;
/**
 *        
 *     ,   :         
 * @author ldy
 *
 */
public abstract class AbstractTask implements Runnable{
	protected String taskName;
}

호출 예시:
package main;

import com.ldy.utils.AbstractTask;

public class TestTask extends AbstractTask{
	public TestTask() {
		this.taskName = "       ";
	}
	@Override
	public void run() {
		try {
			Thread.currentThread().sleep(600);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("      ");
		System.out.println(2/0);
	}

}
package main;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import com.ldy.utils.LazyThreadPool;

public class TestExecutorService {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Future> re = LazyThreadPool.getExecutorService().submit(new TestTask());
		//System.out.println("         :"+re.get());
		//Executors.newFixedThreadPool(1).submit(new TestTask());
	}
}

좋은 웹페이지 즐겨찾기