Scala & Concurrency

Nikhil Anurag

Scala & Concurrency

Who am I?

  • My name is Nikhil
  • Working at Zalando SE
  • Functional Scala - Cats, fs2, http4s
  • Wrote a book, Distributed Computing with Go

Scala & Concurrency

Agenda

  • Concurrency & Parallelism
  • JVM
  • Scala
  • Project Loom

Concurrency vs. Parallelism

Sequential Execution

You

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Split Sequential Execution

You & Roomie

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Watch
TV

Concurrency

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Parallelism

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Watch
TV

Concurrency on JVM

Levels of Abstraction

# Cores

OS Threads

Available
Resources

Ease of Creation

OS Threads

Levels of Abstraction

# Cores

OS Threads

JVM Threads

Available
Resources

Ease of Creation

Levels of Abstraction

# Cores

OS Threads

JVM Threads

Thread Pool

Available
Resources

Ease of Creation

Thread Pools

Thread Pools

Thread

Task

Thread Pools

Thread Pool

Free: 4

Busy: 0

Thread Pools

Task Queue: 4

Thread Pool

Free: 4

Busy: 0

Thread Pools

Task Queue: 3

Thread Pool

Free: 3

Busy: 1

Thread Pools

Task Queue: 2

Thread Pool

Free: 2

Busy: 2

Thread Pools

Task Queue: 1

Thread Pool

Free: 1

Busy: 3

Thread Pools

Task Queue: 0

Thread Pool

Free: 0

Busy: 4

Thread Pools

Task Queue: 0

Thread Pool

Free: 0

Busy: 4

Thread Pools

Task Queue: 1

Thread Pool

Free: 0

Busy: 4

Thread Pools

Task Queue: 1 (Waiting)

Thread Pool

Free: 0

Busy: 4

Thread Pools

Task Queue: 2 (Waiting)

Thread Pool

Free: 0

Busy: 4

Levels of Abstraction

# Cores

OS Threads

JVM Threads

Thread Pool

Available
Resources

Ease of Creation

Concurrency in Scala

Levels of Abstraction

# Cores

OS Threads

JVM Threads

Thread Pool

Fibers

Tasks

Available
Resources

Ease of Creation

Cats Effect IO
&
Monix Tasks

Sequential Execution

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Cats Effect IO

Cats Effect IO

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Cats Effect IO

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Cats IO: Sequential execution on a single thread

Concurrency

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Monix Tasks

Monix Tasks

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Monix Tasks

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Monix Task: Execution with scheduled breaks

Project Loom

Levels of Abstraction

# Cores

OS Threads

JVM Threads

Available
Resources

Ease of Creation

Levels of Abstraction

(Project Loom)

# Cores

OS Threads

Available
Resources

Ease of Creation

# Cores

OS Threads

Available
Resources

Ease of Creation

JVM Threads

Levels of Abstraction

(Project Loom)

# Cores

OS Threads

Available
Resources

Ease of Creation

JVM Threads

Thread Pool

Fibers

Levels of Abstraction

(Project Loom)

Fibers?

Call 1

Email
1/2

Prep
Food

Call 2

Email
2/2

Eat
Food

Questions?

Github: github.com/last-ent

LinkedIn:   Link

Twitter: @last_ent

Blog: last-ent.com

Scala & Concurrency

By last-ent

Scala & Concurrency

  • 346