并发

收藏526

阅读11133

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


相关

视频

RELATED VIDEOS

更多

免费

phpStudy极速入门视频教程

免费

Midjourney基础课程
初级 Midjourney基础课程

11149次学习

收藏

免费

极客学院Git使用视频教程

免费

尚观shell视频教程
高级 尚观shell视频教程

15709次学习

收藏

免费

尚观Linux入门视频教程
初级 尚观Linux入门视频教程

42887次学习

收藏

免费

尚观Linux初级视频教程
初级 尚观Linux初级视频教程

40264次学习

收藏

免费

尚观Linux中级视频教程
中级 尚观Linux中级视频教程

48298次学习

收藏

免费

尚观Linux高级视频教程
高级 尚观Linux高级视频教程

41982次学习

收藏

科技资讯

更多

精选课程

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

共29课时

61.7万人学习

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

共25课时

39.3万人学习

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

共43课时

70.9万人学习

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

共25课时

61.6万人学习

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

共22课时

23万人学习

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

共28课时

33.9万人学习

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

共89课时

125万人学习

关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号