当前位置: 移动技术网 > IT编程>开发语言>Java > Java四种线程池的使用详解

Java四种线程池的使用详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

java通过executors提供四种线程池,分别为:

newcachedthreadpool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

newfixedthreadpool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

newscheduledthreadpool 创建一个定长线程池,支持定时及周期性任务执行。

newsinglethreadexecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(fifo, lifo, 优先级)执行。

(1) newcachedthreadpool

创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。示例代码如下:

package test; 
import java.util.concurrent.executorservice; 
import java.util.concurrent.executors; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 executorservice cachedthreadpool = executors.newcachedthreadpool(); 
 for (int i = 0; i < 10; i++) { 
  final int index = i; 
  try { 
  thread.sleep(index * 1000); 
  } catch (interruptedexception e) { 
  e.printstacktrace(); 
  } 
  cachedthreadpool.execute(new runnable() { 
  public void run() { 
   system.out.println(index); 
  } 
  }); 
 } 
 } 
} 

线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。

(2) newfixedthreadpool

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:

package test; 
import java.util.concurrent.executorservice; 
import java.util.concurrent.executors; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 executorservice fixedthreadpool = executors.newfixedthreadpool(3); 
 for (int i = 0; i < 10; i++) { 
  final int index = i; 
  fixedthreadpool.execute(new runnable() { 
  public void run() { 
   try { 
   system.out.println(index); 
   thread.sleep(2000); 
   } catch (interruptedexception e) { 
   e.printstacktrace(); 
   } 
  } 
  }); 
 } 
 } 
}

因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。

定长线程池的大小最好根据系统资源进行设置。如runtime.getruntime().availableprocessors()

(3)  newscheduledthreadpool

创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下:

package test; 
import java.util.concurrent.executors; 
import java.util.concurrent.scheduledexecutorservice; 
import java.util.concurrent.timeunit; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool(5); 
 scheduledthreadpool.schedule(new runnable() { 
  public void run() { 
  system.out.println("delay 3 seconds"); 
  } 
 }, 3, timeunit.seconds); 
 } 
} 

表示延迟3秒执行。

定期执行示例代码如下:

package test; 
import java.util.concurrent.executors; 
import java.util.concurrent.scheduledexecutorservice; 
import java.util.concurrent.timeunit; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 scheduledexecutorservice scheduledthreadpool = executors.newscheduledthreadpool(5); 
 scheduledthreadpool.scheduleatfixedrate(new runnable() { 
  public void run() { 
  system.out.println("delay 1 seconds, and excute every 3 seconds"); 
  } 
 }, 1, 3, timeunit.seconds); 
 } 
} 

表示延迟1秒后每3秒执行一次。

(4) newsinglethreadexecutor

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(fifo, lifo, 优先级)执行。示例代码如下:

package test; 
import java.util.concurrent.executorservice; 
import java.util.concurrent.executors; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 executorservice singlethreadexecutor = executors.newsinglethreadexecutor(); 
 for (int i = 0; i < 10; i++) { 
  final int index = i; 
  singlethreadexecutor.execute(new runnable() { 
  public void run() { 
   try { 
   system.out.println(index); 
   thread.sleep(2000); 
   } catch (interruptedexception e) { 
   e.printstacktrace(); 
   } 
  } 
  }); 
 } 
 } 
} 

结果依次输出,相当于顺序执行各个任务。

你可以使用jdk自带的监控工具来监控我们创建的线程数量,运行一个不终止的线程,创建指定量的线程,来观察:

工具目录:c:\program files\java\jdk1.6.0_06\bin\jconsole.exe

运行程序做稍微修改:

package test; 
import java.util.concurrent.executorservice; 
import java.util.concurrent.executors; 
public class threadpoolexecutortest { 
 public static void main(string[] args) { 
 executorservice singlethreadexecutor = executors.newcachedthreadpool(); 
 for (int i = 0; i < 100; i++) { 
  final int index = i; 
  singlethreadexecutor.execute(new runnable() { 
  public void run() { 
   try { 
   while(true) { 
    system.out.println(index); 
    thread.sleep(10 * 1000); 
   } 
   } catch (interruptedexception e) { 
   e.printstacktrace(); 
   } 
  } 
  }); 
  try { 
  thread.sleep(500); 
  } catch (interruptedexception e) { 
  e.printstacktrace(); 
  } 
 } 
 } 
} 

效果如下:

选择我们运行的程序:

监控运行状态

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网