Rust 教程

浏览27781
更新时间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() 方法,主线程等待生成的线程完成。


相关视频

更多

免费

phpStudy极速入门视频教程
初级phpStudy极速入门视频教程

54.6万次学习

收藏

免费

phpStudy V8 视频教程
初级phpStudy V8 视频教程

38.8万次学习

收藏

免费

小皮面板使用视频教程
初级小皮面板使用视频教程

21.6万次学习

收藏

免费

好课诞生记
初级好课诞生记

7.1万次学习

收藏

精品课程

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

共29课时

65万人学习

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

共25课时

41.3万人学习

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

共43课时

76.8万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.2万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.4万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.3万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

133.5万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载