您的位置:首页 > 编程语言 > Java开发

Java多线程之线程池的使用示例

2018-01-05 00:00 591 查看

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

TestThreadPool.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThreadPool {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();    //获取开始时间
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);//设置线程池的最大线程数
for (int i = 0; i < 10; i++) {
final int index = i;//一般多线程并发都用final
fixedThreadPool.execute(new Runnable() {
public void run() {
System.out.println(index + "  " + Thread.currentThread().getName());
}
});
}
long endTime = System.currentTimeMillis();    //获取结束时间
System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
}
}

输出结果:

(当然输出结果不是固定的,不过线程数一定不会超过5个)

可以看到 Thread.currentThread().getName() 拿到的name只有5种,说明最大线程数控制在 5 个

第二种:创建一个可缓存的线程池,可以灵活回收空闲线程,若无可回收,则新建线程。

TestThreadPool1.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool1 {
public static void main(String[] args) {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10000; i++) {
final int index = i;
cachedThreadPool.execute(new Runnable() {
public void run() {
System.out.println(index + "  " + Thread.currentThread().getName());
}
});
}
}
}

输出结果的 Thread.currentThread().getName() 拿到的name有一两千种(当然不同环境和配置的机器的结果最大线程数是不同的)

第三种:创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序执行。

TestThreadPool2.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool2 {
public static void main(String[] args) {
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10000; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
public void run() {
System.out.println(index + "  " + Thread.currentThread().getName());
}
});
}
}
}

输出结果:无论循环100次还是100000次,输出结果Thread.currentThread().getName()的值都会是

pool-1-thread-1



第四种:创建一个定长线程池,可以延时或定时周期性地执行任务。

TestThreadPool3.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThreadPool3 {
public static void main(String[] args) {
ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
public void run() {
try {
while(true) {
System.out.println(index + "  " + Thread.currentThread().getName());
Thread.sleep(10 * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}

输出结果:每隔10s就会输出10行结果



**使用 ScheduledExecutorService 的 scheduleAtFixedRate方法可以设置延时和执行间隔

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

Creates and executes a periodic action that becomes enabled first after the given initial delay,
and subsequently with the given period; that is executions will commence after initialDelay
then initialDelay+period, then initialDelay + 2 * period, and so on.

意思是创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。

TestThreadPool4.java

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestThreadPool4 {
public static void main(String[] args) {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
System.out.println(System.currentTimeMillis());
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println(System.currentTimeMillis());
}
}, 5, 10, TimeUnit.SECONDS);
}
}

从输出结果可以看出,延时5s后每隔10s会输出一次当前时间。

**使用ScheduledExecutorService的schedule可以设置首次执行延时

schedule(Runnable command, long delay, TimeUnit unit)

Creates and executes a one-shot action that becomes enabled after the given delay.

创建并执行在给定延迟后启用的一次性操作。

TestThreadPool5.java

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestThreadPool5 {
public static void main(String[] args) {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {
final int index = i;
scheduledThreadPool.schedule(new Runnable() {
public void run() {
System.out.println(index + "  " + Thread.currentThread().getName());
}
}, 3, TimeUnit.SECONDS);
}
}
}

输出结果:运行3s后会输出10行结果,而不会每隔3s输出一行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程池