创建线程池的例子

public class ExecutorTest {
    public static void main(String[] args) {

        //创建线程池的5种方式:
//        Executors.newFixedThreadPool();//创建固定线程数的线程池
//        Executors.newSingleThreadExecutor();//创建单线程的线程池
//        Executors.newCachedThreadPool();//缓存线程池
//        Executors.newScheduledThreadPool();//创建可以延迟执行线程池

        //第5种:
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                16,
                20,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10)
        );
        //建议1:如果任务是属于cpu运算型任务 推荐核心线程为cpu的核数
        //建议2:如果任务是属于io型的 推荐核心线程为cpu的2倍数
        threadPoolExecutor.submit(new Runnable() {
            @Override
            public void run() {
                //任务
                System.out.println("线程池执行任务");
            }
        });




    }
}

面试题:线程池的执行流程:

流程图:

用户提交任务,查看核心线程池是否满,没满则创建线程,执行任务,
满了则交给缓冲队列,缓冲队列没满,则将任务添加到队列,满了,则
判断最大线程池是否满了,没满则创建线程,执行任务,满了则拒绝策略

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的 Linux 内核线程池例子: ``` #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/err.h> #include <linux/slab.h> #include <linux/kthread.h> static struct task_struct *thread_pool[5]; static struct mutex thread_pool_mutex; static int thread_func(void *unused) { int ret = 0; printk(KERN_INFO "Thread [%d] started\n", current->pid); while (!kthread_should_stop()) { mutex_lock(&thread_pool_mutex); /* Do some work */ mutex_unlock(&thread_pool_mutex); } printk(KERN_INFO "Thread [%d] stopping\n", current->pid); return ret; } static int __init example_init(void) { int i, err = 0; mutex_init(&thread_pool_mutex); for (i = 0; i < 5 ; i++) { thread_pool[i] = kthread_create(thread_func, NULL, "pool_thread"); if (IS_ERR(thread_pool[i])) { err = PTR_ERR(thread_pool[i]); thread_pool[i] = NULL; goto exit; } wake_up_process(thread_pool[i]); } printk(KERN_INFO "Example init successful\n"); return 0; exit: printk(KERN_ERR "Failed to create thread_pool[%d]\n", i); while (--i >= 0) { if (thread_pool[i]) { kthread_stop(thread_pool[i]); } } return err; } static void __exit example_exit(void) { int i; mutex_lock(&thread_pool_mutex); for (i = 0; i < 5; i++) { kthread_stop(thread_pool[i]); } mutex_unlock(&thread_pool_mutex); printk(KERN_INFO "Example exit successful\n"); } module_init(example_init); module_exit(example_exit); MODULE_LICENSE("GPL"); ``` 这个例子展示了如何在 Linux 内核中创建线程池。首先,我们创建一个互斥锁以保护线程池。然后,我们使用 `kthread_create` 函数创建 5 个内核线程,并通过 `wake_up_process` 函数启动它们。这些线程会不断循环执行,并在获取到互斥锁之后执行一些工作。最后,在卸载模块时,我们通过 `kthread_stop` 函数停止线程池的所有线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Winter.169

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值