有边界线程池

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011410254/article/details/77942749
public class TestThread {
	 private static ExecutorService executor = null;
    static{
    	executor = new ThreadPoolExecutor(3,
				3, 0L, TimeUnit.MILLISECONDS,
		          new ArrayBlockingQueue<Runnable>(3),
		          new ThreadPoolExecutor.CallerRunsPolicy());
    }
	public void excute( final String name,final int i){
		System.out.println(name);
		executor.execute(new Runnable() {
			int count = 0;
			@Override
			public void run() {
				while (true) {
					count++;
					System.err.println(String.format("-----%s-------------%s------",Thread.currentThread().getName(),name));
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(count>i){
						break;
					}
				}
				
			}
		});
	}
	

}
public class Main {

	public static void main(String[] args) {
        int i = 0;
        TestThread testThread = new TestThread();
		while (true) {
			testThread.excute("test"+i,2*i);
			i++;
			if(i>3)break;
		}
        
	}

}


猜你喜欢

转载自blog.csdn.net/u011410254/article/details/77942749