Java 线程池的基本操作
package com.zhong.thread.threadpool;
import java.util.concurrent.*;
/**
* @ClassName : ThreadPool
* @Description : 线程池的基本操作
* @Author : zhx
* @Date: 2024-02-19 18:03
*/
public class ThreadPool {
public static void main(String[] args) {
// 创建线程池对象
ExecutorService pool = new ThreadPoolExecutor(3,
5,
8,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(4),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
Runnable r = new myRunnable();
Thread t1 = new Thread(r);
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 核心线程达到 3 服用前面的核心线程
pool.execute(t1); // 核心线程达到 3 服用前面的核心线程
pool.execute(t1);
// 等线程池任务执行完毕后关闭
pool.shutdown();
// 直接关闭 并 返回没有执行完的线程 会抛异常
pool.shutdownNow();
}
}
class myRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 红红火火恍恍惚惚");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
文章来源:https://www.toymoban.com/news/detail-831155.html
package com.zhong.thread.threadpool;
import java.util.concurrent.*;
/**
* @ClassName : ThreadPool
* @Description : 线程池的基本操作
* @Author : zhx
* @Date: 2024-02-19 18:03
*/
public class ThreadPool {
public static void main(String[] args) {
// 创建线程池对象
ExecutorService pool = new ThreadPoolExecutor(3,
5,
8,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(4),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
Runnable r = new myRunnable();
Thread t1 = new Thread(r);
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 线程池会自动创建一个线程执行这个任务
pool.execute(t1); // 核心线程达到 3 复用前面的核心线程
pool.execute(t1); // 核心线程达到 3 复用前面的核心线程
pool.execute(t1);
pool.execute(t1);
pool.execute(t1); // 超过最大线程队伍队列 8>4+3 创建新的线程池 现在一共有 4 个线程池
pool.execute(t1); // 超过最大线程队伍队列 9>4+3 创建新的线程池 现在一共有 5 个线程池
// 已经占满 到了新任务的拒绝时机了 会抛出异常
pool.execute(t1);
}
}
class myRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 红红火火恍恍惚惚");
try {
// Thread.sleep(1000);
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
文章来源地址https://www.toymoban.com/news/detail-831155.html
package com.zhong.thread.threadpool;
import java.util.concurrent.*;
/**
* @ClassName : ThreadPool
* @Description : 线程池的基本操作
* @Author : zhx
* @Date: 2024-02-19 18:03
*/
public class ThreadPool {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 创建线程池对象
ExecutorService pool = new ThreadPoolExecutor(3,
5,
8,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(4),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
Future<String> f1 = pool.submit(new myCallable(100));
Future<String> f2 = pool.submit(new myCallable(200));
Future<String> f3 = pool.submit(new myCallable(300));
Future<String> f4 = pool.submit(new myCallable(400));
Future<String> f5 = pool.submit(new myCallable(500));
Future<String> f6 = pool.submit(new myCallable(600));
System.out.println(f1.get());
System.out.println(f2.get());
System.out.println(f3.get());
System.out.println(f4.get());
System.out.println(f5.get());
System.out.println(f6.get());
}
}
/**
* @ClassName : myCallable
* @Description : 创建线程类方法三 实现 Callable 接口返回 1-n 的和
* @Author : zhx
* @Date: 2024-02-19 11:26
*/
class myCallable implements Callable<String> {
private int n;
public myCallable(int n) {
this.n = n;
}
// 重写 call() 方法
@Override
public String call() throws Exception {
int sum = 0;
for (int i = 0; i <= n; i++) {
sum += i;
}
return Thread.currentThread().getName() + "实现了求 1-" + n + " 的值是:" + (sum);
}
}
到了这里,关于Java 线程池的基本操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!