Répertoire d'articles
Utilisation de ThreadPoolExecutor du pool de threads
Utilisation de ThreadPoolExecutor du pool de threads
Le code suivant est basé sur JDK1.8.
package thread;
import java.util.concurrent.*;
/**
* 第四种使用Java多线程的方式,线程池
*/
public class MyThreadPoolDemo {
public static void main(String[] args) {
System.out.println("Fixed Thread Pool");
fixedThreadPool();
System.out.println("Single Thread Pool");
singleThreadPool();
System.out.println("Cached Thread Pool");
cachedThreadPool();
System.out.println("Custom Thread Pool");//自定义的线程池
customThreadPool();
}
private static void customThreadPool() {
ExecutorService threadPool=
new ThreadPoolExecutor(2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName()+"\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void cachedThreadPool() {
//不定量线程
ExecutorService threadPool = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName()+"\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void singleThreadPool() {
//一池1个线程
ExecutorService threadPool = Executors.newSingleThreadExecutor();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void fixedThreadPool() {
//一池5个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5);
//一般常用try-catch-finally
//模拟10个用户来办理业务,每个用户就是一个线程
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
}
tester
public static void main(String[] args) {
System.out.println("Custom Thread Pool");
customThreadPool();
}
private static void customThreadPool() {
ExecutorService threadPool=
new ThreadPoolExecutor(2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName()+"\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
courir
Custom Thread Pool
pool-1-thread-2 办理业务
pool-1-thread-1 办理业务
pool-1-thread-2 办理业务
pool-1-thread-2 办理业务
pool-1-thread-1 办理业务
pool-1-thread-3 办理业务
pool-1-thread-4 办理业务
pool-1-thread-2 办理业务
pool-1-thread-5 办理业务
Lors de la simulation de 15 utilisateurs pour gérer l'entreprise, chaque utilisateur est un thread de demande externe
//模拟15个用户来办理业务,每个用户就是一个来自外部的请求线程
for (int i = 0; i < 15; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName()+"\t办理业务");
});
}
rejectedExecution
Une exception a été lancée à ce moment .
Si aucune exception n'est levée, essayez plusieurs fois.