讲师中心 微信公众号

工具推荐

视频效率加速
AniShort
AniShort

AI短剧协同制作平台

立刻MV
立刻MV

低成本歌曲 MV、视频生成

剧云
剧云

中文短剧剧本制作平台

Seko
Seko

AI短剧一键生成成片

Rust 教程

浏览27988
更新时间2025-08-27

在并发编程中,程序的不同部分独立执行。 另一方面,在并行编程中,程序的不同部分同时执行。 随着越来越多的计算机利用其多个处理器,这两种模型同等重要。

线程

我们可以使用线程来同时运行代码。 在当前的操作系统中,执行程序的代码在一个进程中运行,并且操作系统同时管理多个进程。 在您的程序中,您还可以拥有同时运行的独立部分。 运行这些独立部分的功能称为线程。

创建一个线程

thread::spawn函数用于创建一个新线程。 spawn 函数采用闭包作为参数。 闭包定义了线程应该执行的代码。 以下示例从主线程打印一些文本,并从新线程打印其他文本。

//导入必要的模块
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));
   }
}

输出

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 spawned thread!
hi number 4 from the main thread!

主线程打印从 1 到 4 的值。

注意 − 当主线程结束时,新线程将停止。 该程序的输出每次可能会略有不同。

thread::sleep 函数强制线程在短时间内停止执行,从而允许另一个线程运行。 线程可能会轮流,但这并不能保证——这取决于操作系统如何调度线程。 在此运行中,首先打印主线程,即使来自生成线程的打印语句首先出现在代码中。 此外,即使生成的线程被编程为打印值直到 9,它也只能在主线程关闭之前打印到 5。

连接句柄

生成的线程可能没有机会运行或完全运行。 这是因为主线程完成得很快。 函数 spawn<F, T>(f: F) -> JoinHandlelt;T> 返回一个JoinHandle。 JoinHandle 上的 join() 方法等待关联线程完成。

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

输出

hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 2 from the main thread!
hi number 3 from the spawned thread!
hi number 3 from the main 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!

主线程和派生线程继续切换。

注意 − 由于调用 join() 方法,主线程等待生成的线程完成。


精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时 | 65.3万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时 | 41.5万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时 | 77.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn