Java教程

java线程池

本文主要是介绍java线程池,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package senior;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestPool {
	public static void main(String[] args) {
		ExecutorService service = Executors.newFixedThreadPool(10);
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.execute(new MyThread());
		service.shutdown();
	}
}
class MyThread implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 1; i++) {
			System.out.println(Thread.currentThread().getName()+"\t"+i);
		}
	}
	
}

pool-1-thread-3 0
pool-1-thread-7 0
pool-1-thread-2 0
pool-1-thread-6 0
pool-1-thread-5 0
pool-1-thread-4 0
pool-1-thread-1 0
pool-1-thread-10 0
pool-1-thread-9 0
pool-1-thread-8 0
pool-1-thread-6 0
pool-1-thread-7 0
pool-1-thread-3 0
pool-1-thread-6 0
pool-1-thread-9 0
pool-1-thread-6 0
pool-1-thread-8 0
pool-1-thread-3 0
pool-1-thread-10 0
pool-1-thread-1 0
pool-1-thread-7 0
pool-1-thread-4 0

这篇关于java线程池的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!