rust way step 7


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"] }

相关推荐

  1. CTF <span style='color:red;'>7</span>

    CTF 7

    2024-07-12 20:20:02      51 阅读
  2. ARMday<span style='color:red;'>7</span>

    ARMday7

    2024-07-12 20:20:02      51 阅读
  3. DevOps(7)

    2024-07-12 20:20:02       50 阅读
  4. HCIP-<span style='color:red;'>7</span>

    HCIP-7

    2024-07-12 20:20:02      46 阅读
  5. ARMday<span style='color:red;'>7</span>

    ARMday7

    2024-07-12 20:20:02      38 阅读
  6. <span style='color:red;'>7</span>.string

    7.string

    2024-07-12 20:20:02      31 阅读
  7. MySQL(7

    2024-07-12 20:20:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 20:20:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 20:20:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 20:20:02       58 阅读
  4. Python语言-面向对象

    2024-07-12 20:20:02       69 阅读

热门阅读

  1. sqlalchemy通过查询参数生成query

    2024-07-12 20:20:02       17 阅读
  2. git reset hard和soft的使用和区别

    2024-07-12 20:20:02       19 阅读
  3. 目前分布式光纤测温系统的主流架构有哪些?

    2024-07-12 20:20:02       19 阅读
  4. docker pull 报错:missing signature key,docker版本问题

    2024-07-12 20:20:02       17 阅读
  5. 第六篇:Python元组:不可变序列的魅力

    2024-07-12 20:20:02       18 阅读
  6. Linux rpm和ssh损坏修复

    2024-07-12 20:20:02       21 阅读
  7. 【cnocr的安装使用】

    2024-07-12 20:20:02       20 阅读