16 Advanced concurrent programming

 

This chapter covers

  • Fork / Join
  • Work-stealing algorithms
  • Concurrency and Functional Programming
  • Under the hood with Kotlin coroutines
  • Clojure Concurrency
  • Software transactional memory
  • Agents

In this chapter, we will bring together several themes from earlier chapters.

In particular, we will weave together the functional programming concepts from previous chapters with the Java concurrency libraries from Chapter 5. Our non-Java languages get a look in as well - with some novel concurrency aspects of both Kotlin and Clojure appearing later in the chapter.

[Note]  Note

The concepts in this chapter, such as coroutines and agents (aka actors), are also increasingly part of the landscape of Java concurrency as well.

We’ll kick off with a slight oddity - the Java Fork / Join API. This framework allows a certain class of concurrent problems to be handled more efficiently than the executors we’ve seen in Chapter 6.

16.1 The Fork / Join framework

As we discussed in Chapter 7, processor speeds (or, more properly, transistor counts on CPUs) have increased hugely in recent years. I/O performance has not had the same remarkable improvement, and so the end result is that waiting for I/O is now a very common situation. This suggests that we could make better use of the processing capabilities inside our computers. The Fork / Join (F/J) framework is an attempt to do just that.

16.1.1 A simple F/J example

16.1.2 Parallelizing problems for F/J

16.1.3 Work-stealing algorithms

16.2 Concurrency and functional programming

16.2.1 CompletableFuture revisited

16.2.2 Parallel streams

16.3 Under the hood with Kotlin coroutines

16.3.1 How coroutines work

16.3.2 Coroutine scoping and dispatching

16.4 Concurrent Clojure

16.4.1 Persistent data structures

sitemap