rust way step 7

rust 复制代码
use std::{sync::mpsc, thread, time::Duration};

fn spawn_function() {
    for i in 0..5 {
        println!("spawned thread print {}", i);
        thread::sleep(Duration::from_millis(1));
    }
}

#[tokio::main]
async fn main() {
    thread::spawn(spawn_function);


    thread::spawn(|| {
        for i in 0..5 {
            println!("spawned thread printa {}", i);
            thread::sleep(Duration::from_millis(1));
        }
    });

    





    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let val = String::from("hi............");
        tx.send(val).unwrap();
    });

    let received = rx.recv().unwrap();
    println!("Got: {}", received);



    let s = "hello";
    
    let handle = thread::spawn(move || {
        println!("{}", s);
    });

    handle.join().unwrap();
    thread::sleep(Duration::from_secs(1));


    /*************************************************************************/
    async fn async_task() -> u32 {
       
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
      
       return   999;
    }
    
    // 异步任务执行函数
    async fn execute_async_task() {
        // 调用异步任务,并等待其完成
        let result = async_task().await;
        // 输出结果
        println!("Async task result: {}", result);
    }
    execute_async_task().await;

    println!("Async task completed!");    


    async fn hello() -> String {
        "Hello, world!".to_string()
    }
    async fn print_hello() {
        let result = hello().await;
        println!("{}", result);
    }

    print_hello().await;

    thread::sleep(Duration::from_secs(2));
}

tokio = { version = "1.0.0", features = ["full"] }

相关推荐
Hello_WOAIAI27 分钟前
4.2 python多线程编程:threading 模块深度解析
开发语言·python
2501_9411119936 分钟前
C++中的装饰器模式变体
开发语言·c++·算法
咸甜适中1 小时前
rust语言,将JSON中的所有值以字符串形式存储到sqlite数据库中(逐行注释)
数据库·rust·sqlite·json
树下水月1 小时前
python 连接hive2 数据库
开发语言·数据库·python
Tom4i1 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
凄戚1 小时前
bash和命令
开发语言·chrome·bash
Evan芙1 小时前
Bash 变量命名规则与类型使用
linux·运维·开发语言·chrome·bash
Espresso Macchiato1 小时前
Leetcode 3748. Count Stable Subarrays
算法·leetcode·职场和发展·leetcode hard·leetcode 3748·leetcode周赛476·区间求和
大袁同学1 小时前
【哈希hash】:程序的“魔法索引”,实现数据瞬移
数据结构·c++·算法·哈希算法·散列表