- Learning Concurrent Programming in Scala
- Aleksandar Prokopec
- 305字
- 2021-08-05 17:08:17
Chapter 2. Concurrency on the JVM and the Java Memory Model
"All non-trivial abstractions, to some degree, are leaky."
--Jeff Atwood
Since its inception, Scala has run primarily on top of the JVM, and this fact has driven the design of many of its concurrency libraries. The memory model in Scala, its multithreading capabilities, and its inter-thread synchronization are all inherited from the JVM. Most, if not all, higher-level Scala concurrency constructs are implemented in terms of the low-level primitives presented in this chapter. These primitives are the basic way to deal with concurrency—in a way, the APIs and synchronization primitives in this chapter constitute the assembly of concurrent programming on the JVM.
In most cases, you should avoid low-level concurrency in place of higher-level constructs introduced later, but we felt it important for you to understand what a thread is, that a guarded block is better than busy-waiting, or why a memory model is useful. We are convinced that this is essential for a better understanding of high-level concurrency abstractions. Despite the popular notion that an abstraction that requires knowledge about its implementation is broken, understanding the basics often proves very handy—in practice, all abstractions are to some extent leaky. In what follows, we not only explain the cornerstones of concurrency on the JVM, but also discuss how they interact with some Scala-specific features. In particular, we will cover the following topics in this chapter:
- Creating and starting threads and waiting for their completion
- Communication between threads using object monitors and the
synchronized
statement - How to avoid busy-waiting using guarded blocks
- The semantics of volatile variables
- The specifics of the Java Memory Model (JMM), and why the JMM is important
In the next section, we will study how to use threads: the basic way to express concurrent computations.
- jQuery Mobile Web Development Essentials(Third Edition)
- 自己動手寫搜索引擎
- SOA實踐
- Bootstrap Essentials
- iOS自動化測試實戰(zhàn):基于Appium、Python與Pytest
- Node.js 12實戰(zhàn)
- Mastering Concurrency in Python
- Java EE架構(gòu)設(shè)計與開發(fā)實踐
- Visual Basic程序設(shè)計實驗指導(dǎo)及考試指南
- 零基礎(chǔ)學(xué)Java第2版
- Java編程指南:語法基礎(chǔ)、面向?qū)ο蟆⒑瘮?shù)式編程與項目實戰(zhàn)
- 程序員面試金典(第6版)
- Django 3 Web Development Cookbook
- 你也能看得懂的Python算法書
- Image Processing with ImageJ(Second Edition)