Dzwebs.Net

撰写电脑技术杂文十余年

rust之hreadPool线程池与多线程的使用

Admin | 2023-5-14 15:56:37 | 被阅次数 | 1326

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  如题所示,直接上代码了。

  一、安装库

[dependencies]
threadpool = "1.8.1"

  二、代码

use threadpool::ThreadPool;

fn main() {
    let pool = ThreadPool::new(30);//开启30个线程
    
    for i in 1..=6 {
        pool.execute(move || {
           println!("number {} from the spawned_1 thread!", i); 
        });
    }
    
    for i in 1..=6 {
        pool.execute(move || {
           println!("number {} from the spawned_2 thread!", i); 
        });
    }
    
    for i in 1..=5 {
        println!("number {} from the main thread!", i);
    }
    
    pool.join();
}

  使用线程池的好处是,避免内存过度使用。


该杂文来自: 最新技术

上一篇:rust如何使用example和bin,test运行指定目录的指

下一篇:rust之Arc Mutex Vec String 的合用示例

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计