zoukankan      html  css  js  c++  java
  • rust thread

    https://kaisery.github.io/trpl-zh-cn/ch16-01-threads.html

    use std::thread;
    use std::time::Duration;
    
    fn main() {
        thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    }
       Compiling hello_world v0.1.0 (/data2/rust/thread)
        Finished dev [unoptimized + debuginfo] target(s) in 0.59s
    [root@bogon thread]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/hello_world`
    hi number 1 from the main thread!
    hi number 1 from the spawned thread!
    hi number 2 from the main thread!
    hi number 2 from the spawned thread!
    hi number 3 from the main thread!
    hi number 3 from the spawned thread!
    hi number 4 from the main thread!
    hi number 4 from the spawned thread!
    use std::thread;
    use std::time::Duration;
    
    fn main() {
        let handle = thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    
        handle.join().unwrap();
    }
    [root@bogon thread1]# cargo build
       Compiling hello_world v0.1.0 (/data2/rust/thread1)
        Finished dev [unoptimized + debuginfo] target(s) in 0.58s
    [root@bogon thread1]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/hello_world`
    hi number 1 from the main thread!
    hi number 1 from the spawned thread!
    hi number 2 from the main thread!
    hi number 2 from the spawned thread!
    hi number 3 from the main thread!
    hi number 3 from the spawned thread!
    hi number 4 from the main thread!
    hi number 4 from the spawned thread!
    hi number 5 from the spawned thread!
    hi number 6 from the spawned thread!
    hi number 7 from the spawned thread!
    hi number 8 from the spawned thread!
    hi number 9 from the spawned thread!

    这两个线程仍然会交替执行,不过主线程会由于 handle.join() 调用会等待直到新建线程执行完毕。

    不过让我们看看将 handle.join() 移动到 main 中 for 循环之前会发生什么,如下:

    文件名: src/main.rs

     
    use std::thread;
    use std::time::Duration;
    
    fn main() {
        let handle = thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        handle.join().unwrap();
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    }
    

    主线程会等待直到新建线程执行完毕之后才开始执行 for 循环,所以输出将不会交替出现,如下所示:

     
    hi number 1 from the spawned thread!
    hi number 2 from the spawned thread!
    hi number 3 from the spawned thread!
    hi number 4 from the spawned thread!
    hi number 5 from the spawned thread!
    hi number 6 from the spawned thread!
    hi number 7 from the spawned thread!
    hi number 8 from the spawned thread!
    hi number 9 from the spawned thread!
    hi number 1 from the main thread!
    hi number 2 from the main thread!
    hi number 3 from the main thread!
    hi number 4 from the main thread!
    

    诸如将 join 放置于何处这样的小细节,会影响线程是否同时运行。

  • 相关阅读:
    Vagrant box ubuntu/xenial64 添加vagrant用户解决没有登录密码的问题
    jQuery获取浏览器URL链接的值
    js防止客户端多触发
    Jquery实现一组复选框单选
    jQuery监听文本框值改变触发事件(propertychange)
    将http调用返回json中的有关中文的unicode转换为中文
    Visual Studio 2015 Bowser Link的功能不停的向服务端发送请求
    客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)
    java集群之session共享解决方案
    阻止保存要求重新创建表的更改选项
  • 原文地址:https://www.cnblogs.com/dream397/p/14189581.html
Copyright © 2011-2022 走看看