首页 > 其他分享 >[Rust] Intro Thread: 1. Thread with spawn

[Rust] Intro Thread: 1. Thread with spawn

时间:2024-03-08 20:47:15浏览次数:12  
标签:spawn Thread thread number Intro hi main spawned

We use spawnto create a new thread:

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));
    }
}

Note that when the main thread of a Rust program completes, all spawned threads are shut down, whether or not they have finished running. The output from this program might be a little different every time, but it will look similar to the following:

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!

 

The main thread will always run first, no matter the code order. That's why you see output from main thread first.

If you run this code and only see output from the main thread, or don’t see any overlap, try increasing the numbers in the ranges to create more opportunities for the operating system to switch between the threads.

 

Notice that, we list 1..10for main thread, but it only log until 5, this is due to main thread shut down everything. Will continue with solution to resolve this issue in next blog about join

标签:spawn,Thread,thread,number,Intro,hi,main,spawned
From: https://www.cnblogs.com/Answer1215/p/18061808

相关文章

  • 并发编程Thread的常用API有哪些?
    引言在JDK17(或以上版本)中,Thread类提供了一组常用的API,用于管理线程的创建、启动、暂停、恢复和销毁等操作。本文从api、源码、编程示例等方面详细说明Thread常用函数的使用和注意事项。线程sleep使当前正在执行的线程暂停(挂起)指定的毫秒数。但受系统计时器和调度程序的精度......
  • RT-THREAD的STM32F4系列移植
    RT-Thread:RT-Thread,全称是RealTime-Thread,顾名思义,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,但允许多个任务同时运行并不意味着处理器在同一时刻真的执行了多个任务。事实上,一个处理器核心在某一时刻只能运行一个任务,由于每次对一个任务的执行时间很短、任务......
  • Blazor笔记-Introduce Blazor component
    更新记录注意:非教程。纯笔记,日常查询用的。需要教程的小伙伴找几本书看看即可哈哈,有Vue基础的话非常快,概念都是通的。非工作需要不建议深入学习Blazor,深入Vue吧,用的多,哈哈。完整目录地址:https://www.cnblogs.com/cqpanda/p/17596348.html点击查看2024年3月7日发布。2023......
  • TransmittableThreadLocal 的反复与纠缠
    TransmittableThreadLocal相信很多人用过,一个在多线程情况下共享线程上下文的利器名字好长,以下简称ttl本文以两年前一个真实项目开发遇到的问题,从源码的角度分析并解决环境itemversionjava8springboot2.2.2.RELEASEttl2.11.4代码如下,主线程并行启复......
  • springboot3+vue3(四.2)ThreadLocal优化
    解决痛点:我们在拦截器内已经获取并解析了一遍token数据,如图:然后在获取当前登录用户详细信息时又做了一遍同样的操作,如图:后续如果说需要用到当前登录用户的信息时都要带上token参数,这样是很冗余的。这时就会用到ThreadLocal来进行优化处理。 ThreadLocal工具类/***......
  • C++ Qt开发:运用QThread多线程组件
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍如何运用QThread组件实现多线程功能。多线程技术在程序开发中尤为常用,Qt框架中提供了QThread库来......
  • InheritableThreadLocal 使用举例
    ThreadLocalpublicclassMyThreadextendsThread{privateThreadLocal<String>threadLocal=newThreadLocal<>();publicvoidrun(){threadLocal.set("ThreadLocalvalueinMyThread");System.out.println(&quo......
  • TransmittableThreadLocal 如何解决在分布式环境下线程池中使用ThreadLocal的问题
    在分布式环境下,线程池中使用ThreadLocal会出现线程安全问题,因为线程池中的线程是可以被多个请求共享的,当多个请求同时访问同一个ThreadLocal变量时,会出现数据互相干扰的问题。为了解决这个问题,Java提供了TransmittableThreadLocal类。TransmittableThreadLocal是ThreadLocal的一......
  • 深入理解 ThreadLocal
    目录1.ThreadLocal是什么,它有哪些特性?2.ThreadLocal的底层数据结构包含哪些内容?3.ThreadLocalMap的初始大小、加载因子分别是多少?4.ThreadLocal底层用到的Hash算法是什么?5.ThreadLocal如何解决Hash冲突?6.ThreadLocal底层的扩容机制是什么?7.ThreadLocal的get方法的实现流程?8......
  • ThreadLocal解析
    ThreadLocal解析目录ThreadLocal解析1.两大使用场景——ThreadLocal的用途典型场景1:每个线程需要一个独享的对象(通常是工具类,典型需要使用的类有SimpleDateFormat和Random)典型场景2:每个线程内需要保存全局变量(例如在拦截器中获取用户信息),可以让不同方法直接使用,避免参数传递的麻......