CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
Scala 语言入门/课时/并发与流
RawBlame
课时目录12 节

Scala 语言入门

  1. 01Scala 是什么
  2. 02基础语法
  3. 03OOP 与模式匹配
  4. 04集合与高阶函数
  5. 05函数式编程
  6. 06类型系统
  7. 07构建工具
  8. 08后端框架
  9. 09数据与 JSON
  10. 10测试
  11. 11并发与流
  12. 12综合练习

并发与流

Akka Streams、ZIO Streams、FS2。

30 分钟

并发与流

Scala 并发从 Future(标准库)到 Akka/Pekko Streams、ZIO Streams、FS2(Cats Effect)层层递进。对照 /roadmaps/scala「Concurrency & Streams」节点。

学习目标

  • 使用 Future 与 ExecutionContext 做异步组合
  • 了解 Pekko Streams 的 Source/Flow/Sink 模型
  • 知道 ZIO / FS2 流式处理的基本概念

核心概念

Future 组合

import scala.concurrent.{Future, ExecutionContext}
import scala.util.{Success, Failure}

def fetchTitle(id: Long)(using ec: ExecutionContext): Future[String] =
  Future {
    if id <= 0 then throw new IllegalArgumentException("bad id")
    else s"Title-$id"
  }

def fetchMany(ids: List[Long])(using ec: ExecutionContext): Future[List[String]] =
  Future.sequence(ids.map(fetchTitle))

given ec: ExecutionContext = ExecutionContext.global

val result = fetchMany(List(1L, 2L, 3L))
result.onComplete {
  case Success(xs) => println(xs.mkString(", "))
  case Failure(e)  => println(s"error: ${e.getMessage}")
}
Thread.sleep(500)

Future.sequence 并行多个 Future;生产环境应配置专用线程池而非 global。

Pekko Streams 概览

import org.apache.pekko.actor.typed.{ActorSystem, Behaviors}
import org.apache.pekko.stream.scaladsl.{Source, Sink, Flow}
import org.apache.pekko.stream.Materializer

given system: ActorSystem[Nothing] = ActorSystem(Behaviors.empty, "streams")
given mat: Materializer = Materializer(system)

val stream =
  Source(1 to 100)
    .filter(_ % 2 == 0)
    .map(_ * 10)
    .runWith(Sink.seq)

stream.foreach(seq => println(s"count=${seq.size}, head=${seq.headOption}"))

Source 产数据、Flow 变换、Sink 消费;背压(backpressure)由 Stream 协议处理。

FS2 / ZIO Streams(概念)

// FS2 风格片段
import fs2.Stream

val s: Stream[IO, Int] =
  Stream.emits(1 to 5).filter(_ > 2).map(_ * 2)

// ZIO Stream
import zio.stream.*

val z: ZStream[Any, Throwable, String] =
  ZStream.fromIterable(1 to 3).map(n => s"item-$n")

FS2 与 ZIO Streams 纯函数式、资源安全(bracket),适合 Kafka / 文件管道。

与 Java 对照

ScalaJava
Future
CompletableFuture
Pekko StreamsReactive Streams / Flow API(Java 9+)
Actor无内置;Loom Virtual Thread 不同模型
FS2 / ZIOProject Reactor(Spring WebFlux)

Java Virtual Thread 简化阻塞代码;Scala Actor/Stream 强调消息与背压,选型取决于团队范式。

练习

  • 用 Future 实现超时:Future.firstCompletedOf 或 recover
  • 写 Pekko Stream 将行文件读入并计数
  • 对比 Java CompletableFuture.allOf 与 Future.sequence

小结

Future 入门,Pekko Streams 管背压,FS2/ZIO 管 FP 流式管道。 下一课综合练习。见 scala-essentials。

加载本节练习…
← 上一课测试下一课 →综合练习