Every time you start a new thread should create a waste of resources, there are too many threads when the time will cause the server to crash, all have to manage the thread pool thread, here are several ways to create a common thread:
First, create a thread pool size is not fixed
This is a thread pool having a buffer function, the system creates a thread as needed. Thread is buffered to the thread pool, if the thread pool size exceeds the required processing tasks thread, the thread pool thread will recover when the task increases, can increase the thread pool thread to handle the task, the thread pool thread will not size limit, the size of the thread pool depends on the operating system.
public class CacheThreadPoolTest { public static void main(String[] args) { ExecutorService es = Executors.newCachedThreadPool(); for (int i = 0; i <10 ; i++) { CacheThreadPoolTest.Threadchi threadchi = new CacheThreadPoolTest.Threadchi(); es.execute(threadchi); } es.shutdown(); } private static class Threadchi implements Runnable{ @Override public void run() { for (int i = 0; i <10 ; i++) { System.out.println(Thread.currentThread().getName()+":"+i); } } } }
Second, create a fixed number of threads in the thread pool
Has created a reusable, there are a fixed number of thread pool. Each time you submit a task to submit a thread, the thread knows reach the thread pool size will not create a new thread, the thread pool size reaches the maximum after the stable and unchanging, and if a site is aborted, it will create a new thread.
/** * @author zhengzheng */ public class FixedThreadPoolTest { public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(3); for (int i = 0; i <10 ; i++) { es.execute(()->{ for (int j = 0; j <2 ; j++) { System.out.println(Thread.currentThread().getName()+":"+j); } }); } es.shutdown(); } }
Third, create a single-threaded thread pool
Only create a thread pool thread, commit the order, to keep up with the number 1 is the same.
public class SingleThreadExecutorTest { public static void main(String[] args) { ExecutorService es = Executors.newSingleThreadExecutor(); for (int i = 0; i <5 ; i++) { MyThread thread = new MyThread(); es.execute(thread); } es.shutdown(); } private static class MyThread implements Runnable{ @Override public void run() { for (int i = 0; i <3 ; i++) { System.out.println(Thread.currentThread().getName()+":"+i); } } } }
Fourth, create a timer thread
Create a thread pool size can be set. This thread support and regular periodic tasks.
/** * @author zhengzheng046 */ public class ScheduledThreadPoolTest { public static void main(String[] args) { MyThread task = new MyThread(); ScheduledExecutorService es = Executors.newScheduledThreadPool(2); //参数1:目标对象 参数2:隔多长时间开始执行线程, 参数3:执行周期 参数4:时间单位 es.scheduleAtFixedRate(task,2,4, TimeUnit.SECONDS); //注意.不能执行es.shutdown().否则线程池立即关闭 //es.shutdown(); } private static class MyThread implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"开始执行任务"+System.currentTimeMillis()); } } }