一、ThreadPoolExecutor.execute
public class CustomThreadPool {
public static ThreadPoolExecutor getThreadPoolExecutor() {
AtomicInteger threadNumber = new AtomicInteger();
return new ThreadPoolExecutor(4,
8,
1,
TimeUnit.MINUTES,
new ArrayBlockingQueue<>(1000),
r -> {
Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
if (thread.isDaemon()) {
thread.setDaemon(true);
}
return thread;
});
}
public static ThreadPoolExecutor getThreadPoolExecutorNoLambda() {
AtomicInteger threadNumber = new AtomicInteger();
return new ThreadPoolExecutor(4,
8,
1,
TimeUnit.MINUTES,
new ArrayBlockingQueue<>(1000),
new ThreadFactory() {
@Override
public Thread newThread(@NotNull Runnable r) {
Thread thread = new Thread(r, "jak-custom-thread-pool-" + threadNumber.getAndIncrement());
if (thread.isDaemon()) {
thread.setDaemon(false);
}
return null;
}
});
}
private static void asyncPrint() {
System.out.println(Thread.currentThread().getName() + "-jak");
}
public static void main(String[] args) throws InterruptedException {
System.out.println("开始");
ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
for (int i = 0; i <= 5; i++) {
threadPoolExecutor.execute(() -> asyncPrint());
}
Thread.sleep(1000);
System.out.println("结束");
}
}
public class CustomThreadPool {
public static ThreadPoolExecutor getThreadPoolExecutor() {
AtomicInteger threadNumber = new AtomicInteger();
return new ThreadPoolExecutor(4,
8,
1,
TimeUnit.MINUTES,
new ArrayBlockingQueue<>(1000),
r -> {
Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
if (thread.isDaemon()) {
thread.setDaemon(true);
}
return thread;
});
}
public static void main(String[] args) throws InterruptedException {
System.out.println("开始");
ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
for (int i = 0; i <= 5; i++) {
AsyncPrint4Runnable asyncPrint4Runnable = new AsyncPrint4Runnable();
threadPoolExecutor.execute(asyncPrint4Runnable);
}
Thread.sleep(1000);
System.out.println("结束");
}
}
class AsyncPrint4Runnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-jak");
}
}
二、ThreadPoolExecutor.submit
public class CustomThreadPool {
public static ThreadPoolExecutor getThreadPoolExecutor() {
AtomicInteger threadNumber = new AtomicInteger();
return new ThreadPoolExecutor(4,
8,
1,
TimeUnit.MINUTES,
new ArrayBlockingQueue<>(1000),
r -> {
Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
if (thread.isDaemon()) {
thread.setDaemon(true);
}
return thread;
});
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("开始");
ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
for (int i = 0; i <= 5; i++) {
AsyncPrint4Runnable asyncPrint4Runnable = new AsyncPrint4Runnable();
Future<String> future = threadPoolExecutor.submit(asyncPrint4Runnable);
String threadName = future.get();
System.out.println("future: " + threadName);
}
Thread.sleep(1000);
System.out.println("结束");
}
}
class AsyncPrint4Runnable implements Callable<String> {
@Override
public String call() throws Exception {
String threadName = Thread.currentThread().getName() + "-jak";
System.out.println(threadName);
return threadName;
}
}
public class MyThreadPoolDemo {
public static ThreadPoolExecutor getThreadPoolExecutor() {
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
return threadPool;
}
public static void main(String[] args) {
ThreadPoolExecutor threadPool = getThreadPoolExecutor();
try {
//模拟10个用户来办理业务,每个用户就是一个来自外部的请求线程
for (int i=1; i <=9; i++) {
Future<String> future = threadPool.submit(() -> {
String str = "jak";
System.out.println(str);
return str;
});
String futureResult = future.get();
System.out.println("futrure" + futureResult);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
}
Java线程池详解、Java创建线程的方式