Reader small image

You're reading from  The Clojure Workshop

Product typeBook
Published inJan 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781838825485
Edition1st Edition
Languages
Right arrow
Authors (5):
Joseph Fahey
Joseph Fahey
author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

Thomas Haratyk
Thomas Haratyk
author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

Scott McCaughie
Scott McCaughie
author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

Yehonathan Sharvit
Yehonathan Sharvit
author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

Konrad Szydlo
Konrad Szydlo
author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo

View More author details
Right arrow

12. Concurrency

Overview

In this chapter we will explore Clojure's concurrency features. On the JVM, you will learn the basics of programming with multiple processor threads: starting a new thread and using the results. To coordinate your threads, we will use Clojure's innovative reference types. One of these, the atom, can also be used in a JavaScript environment.

By the end of this chapter, you will be able to build simple browser games and manage their state using atoms.

Introduction

Ever since the Clojure language was first introduced, its concurrency model has been one of its major selling points. In programming, the word "concurrency" can apply to a lot of different situations. To start with a simple definition, any time your program or your system has more than one simultaneous flow of operations, you are dealing with concurrency. In multithreaded Java programs, that would mean code running simultaneously in separate processor threads. Each processor thread follows its own internal logic, but to work properly your program needs to coordinate the communication between the different threads. Even though JavaScript runtimes are single-threaded, both the browser and Node.js environments have their own ways of dealing with simultaneous logical flows. While the roots of Clojure's concurrency are definitely in Java, some of the ideas and tools apply equally in ClojureScript.

In this chapter, you will learn the basics of concurrent programming...

Concurrency in General

Modern computers use threads to distribute execution between multiple processor cores. There are many reasons for this, including the physics of microchip design and the need for user environments that remain responsive even when one program is performing an intensive computation in the background. Everyone wants to be able to check their email, listen to music, and run their Clojure REPL at the same time! Inside a program, this kind of multitasking can also represent a significant performance gain. While one thread is waiting for data from the disk drive, another for the network, two other threads can be processing data. When done correctly, this can represent a significant gain in performance and overall efficiency. The operative phrase here, though, is "when done correctly." Concurrency can be tricky.

Most computer code is written in a linear manner: do this, do that, then do this. The source code for a method or a function reads from top...

Futures

With pmap, Clojure takes care of all the thread management for you, which makes things easy. A lot of times, however, you need more control over your threads than what pmap provides. Clojure's futures do just this. They are a mechanism for spawning and waiting for new threads.

Consider a situation where two expensive calculations are needed to perform a third operation, such as adding the two results together. In a single-threaded context, we would just write this:

(+ (expensive-calc-1 5) (expensive-calc-2 19))

Written this way, the call to expensive-calc-1 needs to complete before expensive-calc-2 can start. If the calculations could be run in parallel, we would cut the execution time nearly in half, in the best cases. Running the two threads in parallel creates some new problems, though. We need a way of coordinating the return values, especially since we don't know whether expensive-calc-1 or expensive-calc-2 will complete first. We need a way to wait...

Coordination

Futures work well for cases like the crowdspell example. Work is assigned to a thread; the thread performs its task independently and returns a result to the initial thread. The coordination is in the gathering of the results: evaluation is blocked until all the futures have completed. Thanks to immutability, there is a guarantee that simultaneous threads won't interfere with each other because nothing is shared.

This simple model is effective precisely because it is simple. Sometimes, however, more coordination is necessary, especially when communication between threads is required.

With a future, we fork, perform a computation, and return the data:

Figure 12.6: With a future, coordination occurs when the future is dereferenced

Message sending is one way to communicate among threads. Now, we imagine three threads that send messages to one another:

Figure 12.7: Complex interactions between multiple threads

To...

Summary

Concurrency, by its very nature, is a complex problem. While it's impossible to cover all the techniques you might need, hopefully, this chapter will provide you with the tools to get started. We covered the usage of pmap and future for using multiple threads. We also saw Clojure's reference types: var, atoms, agents, and refs. We used atoms to manage state in a browser-based ClojureScript application.

For each of these topics, there is a lot more that can be said. What you learn further down the road will depend on the kinds of problems you need to solve. Concurrency is one of the areas where the problems will be more diverse than almost any other. Familiarity with Clojure's basic approach to these questions will start you in the right direction when you search for solutions.

In the next chapter, we will take another big step toward real-world Clojure by learning how to interact with databases.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Clojure Workshop
Published in: Jan 2020Publisher: PacktISBN-13: 9781838825485
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (5)

author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo