Java自带线程池


package thread;

import org.junit.Test;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by guofeipeng on 15/11/17.
 * 线程池测试
 */
public class TestThreadPool {

    @Test
    public void testThradPool(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(30, 50,  60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());
        for(int i = 0 ; i < 10 ; i++){
            Thread t = new Thread(new Task());
            t.setName("thread" + i);
            threadPoolExecutor.execute(t);
        }
        threadPoolExecutor.shutdown();
        while (!threadPoolExecutor.isTerminated()) {

        }
    }


    class Task implements Runnable{
        @Override
        public void run() {
            System.out.println("task " + Thread.currentThread().getName());
        }
    }
}








版权声明:本文为Mooner_guo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。