package learn.threadpool; public class MemberService extends Thread { @Override public void run() { long l1=System.currentTimeMillis(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } long l2=System.currentTimeMillis(); System.out.println("member service...."+(l2-l1)); } }
package learn.threadpool; public class OrderService extends Thread { @Override public void run() { long l1=System.currentTimeMillis(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } long l2=System.currentTimeMillis(); System.out.println("order service...."+(l2-l1)); } }
package learn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestMain { public static void main(String[] args) { ExecutorService pool= Executors.newCachedThreadPool(); Thread t1=new MemberService(); Thread t2=new OrderService(); long l1=System.currentTimeMillis(); pool.execute(t1); pool.execute(t2); pool.shutdown(); long l2=System.currentTimeMillis(); System.out.println(l2-l1); } }
执行结果 :
1
member service....2000
order service....2000
可以发现后两句话是同时执行的
newCachedThreadPool
创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程, 那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。
此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
package learn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestMain { public static void main(String[] args) { ExecutorService pool= Executors.newFixedThreadPool(1); Thread t1=new MemberService(); Thread t2=new OrderService(); long l1=System.currentTimeMillis(); pool.execute(t1); pool.execute(t2); pool.shutdown(); long l2=System.currentTimeMillis(); System.out.println(l2-l1); } }
执行结果:
1
member service....2000
order service....2000
可以发现member service....2000先执行,再2s后执行order service....2000
newFixedThreadPool
创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
package learn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestMain { public static void main(String[] args) { ExecutorService pool= Executors.newSingleThreadExecutor(); Thread t1=new MemberService(); Thread t2=new OrderService(); long l1=System.currentTimeMillis(); pool.execute(t2); pool.execute(t1); pool.shutdown(); long l2=System.currentTimeMillis(); System.out.println(l2-l1); } }
执行结果:
1
member service....2000
order service....2000
可以发现member service....2000先执行,再2s后执行order service....2000
创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。
如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。这里就不演示了。
如何在线程池中的线程都执行完毕后再实现某些方法
package learn.threadpool; import java.util.concurrent.*; public class TestMain { public static void main(String[] args) { ExecutorService pool=Executors.newFixedThreadPool(10); pool.execute(new MemberService()); pool.execute(new OrderService()); pool.shutdown(); while(true){ if(pool.isTerminated()){ System.out.println("线程池中的线程执行完毕"); break; } } } }
输出结果:
order service....2000
member service....2000
线程池中的线程执行完毕
用countDownLatch方式:
package learn.threadpool; import java.util.concurrent.CountDownLatch; public class MemberService extends Thread { private CountDownLatch cdl; public MemberService(CountDownLatch cdl) { this.cdl = cdl; } @Override public void run() { long l1=System.currentTimeMillis(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }finally { cdl.countDown(); } long l2=System.currentTimeMillis(); System.out.println("member service...."+(l2-l1)); } }
package learn.threadpool; import java.util.concurrent.*; public class TestMain { public static void main(String[] args) throws InterruptedException { final int threadNum=10; ExecutorService pool=Executors.newFixedThreadPool(threadNum); final CountDownLatch cdl=new CountDownLatch(threadNum); for(int i=0;i<10;i++){ Thread t=new MemberService(cdl); pool.execute(t); } cdl.await(); System.out.println("end..."); } }
CountDownLatch性能很差
更优雅的方法:
package learn.threadpool; import java.util.concurrent.*; public class TestMain { public static void main(String[] args) throws InterruptedException { final int threadNum=10; ExecutorService pool=Executors.newFixedThreadPool(threadNum); pool.execute(new OrderService()); pool.execute(new MemberService()); pool.shutdown(); pool.awaitTermination(1, TimeUnit.HOURS); System.out.println("end..."); } }
执行结果:
member service....2001
order service....2001
end...
awaitTermination(long timeOut, TimeUnit unit)
当前线程阻塞,直到
- 等所有已提交的任务(包括正在跑的和队列中等待的)执行完
- 或者等超时时间到
- 或者线程被中断,抛出
InterruptedException