使用线程池的好处有很多,比如节省系统资源的开销,节省创建和销毁线程的时间等,当我们需要处理的任务较多时,就可以使用线程池,可能还有很多用户不知道Java线程池如何使用?下面小编给大家分享Java四种线程池的使用方法。
线程池介绍:
线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间后创建另一个辅助线程但线程的数目永远不会超过最大值。超过最大值的线程可以排队,但他们要等到其他线程完成后才启动。
Java四种线程池的使用:
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
(1) newCachedThreadPool
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。示例代码如下:
- 01package test;
- 02import java.util.concurrent.ExecutorService;
- 03import java.util.concurrent.Executors;
- 04public class ThreadPoolExecutorTest {
- 05public static void main(String[] args) {
- 06ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
- 07for (int i = 0; i < 10; i++) {
- 08final int index = i;
- 09try {
- 10Thread.sleep(index * 1000);
- 11} catch (InterruptedException e) {
- 12e.printStackTrace();
- 13}
- 14cachedThreadPool.execute(new Runnable() {
- 15public void run() {
- 16System.out.println(index);
- 17}
- 18});
- 19}
- 20}
- 21}
复制代码
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
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:
- 01package test;
- 02import java.util.concurrent.ExecutorService;
- 03import java.util.concurrent.Executors;
- 04public class ThreadPoolExecutorTest {
- 05public static void main(String[] args) {
- 06ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
- 07for (int i = 0; i < 10; i++) {
- 08final int index = i;
- 09fixedThreadPool.execute(new Runnable() {
- 10public void run() {
- 11try {
- 12System.out.println(index);
- 13Thread.sleep(2000);
- 14} catch (InterruptedException e) {
- 15e.printStackTrace();
- 16}
- 17}
- 18});
- 19}
- 20}
- 21}
复制代码
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
创建一个定长线程池,支持定时及周期性任务执行。延迟执行示例代码如下:
- 01package test;
- 02import java.util.concurrent.Executors;
- 03import java.util.concurrent.ScheduledExecutorService;
- 04import java.util.concurrent.TimeUnit;
- 05public class ThreadPoolExecutorTest {
- 06public static void main(String[] args) {
- 07ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
- 08scheduledThreadPool.schedule(new Runnable() {
- 09public void run() {
- 10System.out.println("delay 3 seconds");
- 11}
- 12}, 3, TimeUnit.SECONDS);
- 13}
- 14}
复制代码
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秒执行。
定期执行示例代码如下:
- 01package test;
- 02import java.util.concurrent.Executors;
- 03import java.util.concurrent.ScheduledExecutorService;
- 04import java.util.concurrent.TimeUnit;
- 05public class ThreadPoolExecutorTest {
- 06public static void main(String[] args) {
- 07ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
- 08scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
- 09public void run() {
- 10System.out.println("delay 1 seconds, and excute every 3 seconds");
- 11}
- 12}, 1, 3, TimeUnit.SECONDS);
- 13}
- 14}
复制代码
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, 优先级)执行。示例代码如下:
- 01package test;
- 02import java.util.concurrent.ExecutorService;
- 03import java.util.concurrent.Executors;
- 04public class ThreadPoolExecutorTest {
- 05public static void main(String[] args) {
- 06ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
- 07for (int i = 0; i < 10; i++) {
- 08final int index = i;
- 09singleThreadExecutor.execute(new Runnable() {
- 10public void run() {
- 11try {
- 12System.out.println(index);
- 13Thread.sleep(2000);
- 14} catch (InterruptedException e) {
- 15e.printStackTrace();
- 16}
- 17}
- 18});
- 19}
- 20}
- 21}
复制代码
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
运行程序做稍微修改:
- 01package test;
- 02import java.util.concurrent.ExecutorService;
- 03import java.util.concurrent.Executors;
- 04public class ThreadPoolExecutorTest {
- 05public static void main(String[] args) {
- 06ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();
- 07for (int i = 0; i < 100; i++) {
- 08final int index = i;
- 09singleThreadExecutor.execute(new Runnable() {
- 10public void run() {
- 11try {
- 12while(true) {
- 13System.out.println(index);
- 14Thread.sleep(10 * 1000);
- 15}
- 16} catch (InterruptedException e) {
- 17e.printStackTrace();
- 18}
- 19}
- 20});
- 21try {
- 22Thread.sleep(500);
- 23} catch (InterruptedException e) {
- 24e.printStackTrace();
- 25}
- 26}
- 27}
- 28}
复制代码
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();
}
}
}
}
效果如下:
选择我们运行的程序:
监控运行状态
关于Java四种线程池的使用技巧就给大家分享到这里了,正所谓工欲善其事,必先利其器,我们掌握了诀窍,处理事情才能事倍功半,希望可以帮助到大家。
发表评论
共0条
评论就这些咯,让大家也知道你的独特见解
立即评论以上留言仅代表用户个人观点,不代表系统之家立场