首页 > 开发 > Java > 正文

Java编程中线程池的基本概念和使用

2024-07-13 09:56:08
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Java编程中线程池的基本概念和使用,多线程编程是使Java程序实现并发的一个重要手段,需要的朋友可以参考下

1 引入线程池的原因

由于线程的生命周期中包括创建、就绪、运行、阻塞、销毁阶段,当我们待处理的任务数目较小时,我们可以自己创建几个线程来处理相应的任务,但当有大量的任务时,由于创建、销毁线程需要很大的开销,运用线程池这些问题就大大的缓解了。

2 线程池的使用

我们只需要运用Executors类给我们提供的静态方法,就可以创建相应的线程池:

 

 
  1. public static ExecutorSevice newSingleThreadExecutor() 
  2.  
  3. public static ExecutorSevice newFixedThreadPool() 
  4.  
  5. public static ExecutorSevice newCachedThreadPool() 

newSingleThreadExecutor返回以个包含单线程的Executor,将多个任务交给此Exector时,这个线程处理完一个任务后接着处理下一个任务,若该线程出现异常,将会有一个新的线程来替代。

newFixedThreadPool返回一个包含指定数目线程的线程池,如果任务数量多于线程数目,那么没有没有执行的任务必须等待,直到有任务完成为止。

newCachedThreadPool根据用户的任务数创建相应的线程来处理,该线程池不会对线程数目加以限制,完全依赖于JVM能创建线程的数量,可能引起内存不足。

我们只需要将待执行的任务放入run方法中即可,将Runnable接口的实现类交给线程池的execute方法,作为它的一个参数,如下所示:

 

 
  1. Executor executor = Executors.newSingleThreadExecutor(); 
  2. executor.execute(new Runnable(){ 
  3. public void run(){ 
  4. //执行的任务  

如果需要给任务传递参数,可以通过创建一个Runnable接口的实现类来完成。

3.实例

(1):newSingleThreadExecutor

MyThread.java

 

 
  1. publicclassMyThread extends Thread { 
  2. @Override 
  3. publicvoid run() { 
  4. System.out.println(Thread.currentThread().getName() + "正在执行。。。"); 
  5. TestSingleThreadExecutor.java 
  6. publicclassTestSingleThreadExecutor { 
  7. publicstaticvoid main(String[] args) { 
  8. //创建一个可重用固定线程数的线程池 
  9. ExecutorService pool = Executors. newSingleThreadExecutor(); 
  10. //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 
  11. Thread t1 = new MyThread(); 
  12. Thread t2 = new MyThread(); 
  13. Thread t3 = new MyThread(); 
  14. Thread t4 = new MyThread(); 
  15. Thread t5 = new MyThread(); 
  16. //将线程放入池中进行执行 
  17. pool.execute(t1); 
  18. pool.execute(t2); 
  19. pool.execute(t3); 
  20. pool.execute(t4); 
  21. pool.execute(t5); 
  22. //关闭线程池 
  23. pool.shutdown(); 

输出结果

 

 
  1. pool-1-thread-1正在执行。。。 
  2. pool-1-thread-1正在执行。。。 
  3. pool-1-thread-1正在执行。。。 
  4. pool-1-thread-1正在执行。。。 
  5. pool-1-thread-1正在执行。。。 

(2):newFixedThreadPool

TestFixedThreadPool.Java

 

 
  1. publicclass TestFixedThreadPool { 
  2. publicstaticvoid main(String[] args) { 
  3. //创建一个可重用固定线程数的线程池 
  4. ExecutorService pool = Executors.newFixedThreadPool(2); 
  5. //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 
  6. Thread t1 = new MyThread(); 
  7. Thread t2 = new MyThread(); 
  8. Thread t3 = new MyThread(); 
  9. Thread t4 = new MyThread(); 
  10. Thread t5 = new MyThread(); 
  11. //将线程放入池中进行执行 
  12. pool.execute(t1); 
  13. pool.execute(t2); 
  14. pool.execute(t3); 
  15. pool.execute(t4); 
  16. pool.execute(t5); 
  17. //关闭线程池 
  18. pool.shutdown(); 

输出结果

 

 
  1. pool-1-thread-1正在执行。。。 
  2. pool-1-thread-2正在执行。。。 
  3. pool-1-thread-1正在执行。。。 
  4. pool-1-thread-2正在执行。。。 
  5. pool-1-thread-1正在执行。。。 

(3): newCachedThreadPool

TestCachedThreadPool.java

 

 
  1. publicclass TestCachedThreadPool { 
  2. publicstaticvoid main(String[] args) { 
  3. //创建一个可重用固定线程数的线程池 
  4. ExecutorService pool = Executors.newCachedThreadPool(); 
  5. //创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口 
  6. Thread t1 = new MyThread(); 
  7. Thread t2 = new MyThread(); 
  8. Thread t3 = new MyThread(); 
  9. Thread t4 = new MyThread(); 
  10. Thread t5 = new MyThread(); 
  11. //将线程放入池中进行执行 
  12. pool.execute(t1); 
  13. pool.execute(t2); 
  14. pool.execute(t3); 
  15. pool.execute(t4); 
  16. pool.execute(t5); 
  17. //关闭线程池 
  18. pool.shutdown(); 

输出结果:

 

 
  1. pool-1-thread-2正在执行。。。 
  2. pool-1-thread-4正在执行。。。 
  3. pool-1-thread-3正在执行。。。 
  4. pool-1-thread-1正在执行。。。 
  5. pool-1-thread-5正在执行。。。 

(4):newScheduledThreadPool

TestScheduledThreadPoolExecutor.java

 

 
  1. publicclass TestScheduledThreadPoolExecutor { 
  2. publicstaticvoid main(String[] args) { 
  3. ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); 
  4. exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常 
  5. @Override 
  6. publicvoid run() { 
  7. //throw new RuntimeException(); 
  8. System.out.println("================"); 
  9. }, 1000, 5000, TimeUnit.MILLISECONDS); 
  10. exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的 
  11. @Override 
  12. publicvoid run() { 
  13. System.out.println(System.nanoTime()); 
  14. }, 1000, 2000, TimeUnit.MILLISECONDS); 

输出结果

 

 
  1. ================ 
  2. 8384644549516 
  3. 8386643829034 
  4. 8388643830710 
  5. ================ 
  6. 8390643851383 
  7. 8392643879319 
  8. 8400643939383 


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表