Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Concurrent Programming in Scala

You're reading from   Learning Concurrent Programming in Scala Dive into the Scala framework with this programming guide, created to help you learn Scala and to build intricate, modern, scalable concurrent applications

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher Packt
ISBN-13 9781783281411
Length 366 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Aleksandar Prokopec Aleksandar Prokopec
Author Profile Icon Aleksandar Prokopec
Aleksandar Prokopec
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introduction FREE CHAPTER 2. Concurrency on the JVM and the Java Memory Model 3. Traditional Building Blocks of Concurrency 4. Asynchronous Programming with Futures and Promises 5. Data-Parallel Collections 6. Concurrent Programming with Reactive Extensions 7. Software Transactional Memory 8. Actors 9. Concurrency in Practice Index

What you need for this book

In this section, we describe some of the requirements that are necessary to read and understand this book. We explain how to install the Java Development Kit that is required to run Scala programs, and show how to use Simple Build Tool to run various examples.

We will not require an IDE in this book. The program that you use to write code is entirely up to you, and you can choose anything, such as Vim, Emacs, Sublime Text, Eclipse, IntelliJ IDEA, Notepad++, or some other text editor.

Installing the JDK

Scala programs are not compiled directly to the native machine code, so they cannot be run as executables on various hardware platforms. Instead, the Scala compiler produces an intermediate code format, called the Java bytecode. To run this intermediate code, your computer must have the Java Virtual Machine software installed. In this section, we explain how to download and install the Java Development Kit, which includes the Java Virtual Machine and other useful tools.

There are multiple implementations of the JDK that are available from different software vendors. We recommend that you use the Oracle JDK distribution. To download and install the Java Development Kit, follow these steps:

  1. Open the following URL in your web browser: www.oracle.com/technetwork/java/javase/downloads/index.html.
  2. If you cannot open the specified URL, go to your search engine and enter the keywords JDK Download.
  3. Once you find the link for the Java SE download on the Oracle website, download the appropriate version of JDK 7 for your operating system: Windows, Linux, or Mac OS X; 32-bit or 64-bit.
  4. If you are using the Windows operating system, simply run the installer program. If you are using the Mac OS X, open the dmg archive to install JDK. Finally, if you are using Linux, decompress the archive to a XYZ directory, and add the bin subdirectory to the PATH variable:
    export PATH=XYZ/bin:$PATH
  5. You should now be able to run the java and javac commands in the terminal. Enter javac to see if it is available (you will never invoke this command directly in this book, but running it verifies that it is available):
    javac
    

It is possible that your operating system already has JDK installed. To verify this, simply run the javac command, as in the last step in the preceding description.

Installing and using SBT

Simple Build Tool (SBT) is a command-line build tool used for Scala projects. Its purpose is to compile Scala code, manage dependencies, continuous compilation and testing, deployment, and many other uses. Throughout this book, we will use SBT to manage our project dependencies and run example code.

To install SBT, please follow these instructions:

  1. Go to the http://www.scala-sbt.org/ URL.
  2. Download the installation file for your platform. If you are running on Windows, this is the msi installer file. If you are running on Linux or OS X, this is the zip or tgz archive file.
  3. Install SBT. If you are running on Windows, simply run the installer file. If you are running on Linux or OS X, unzip the contents of the archive in your home directory.

You are now ready to use SBT. In the following steps, we will create a new SBT project:

  1. Open a command prompt if you are running on Windows, or a terminal window if you are running on Linux or OS X.
  2. Create an empty directory called scala-concurrency-examples:
    $ mkdir scala-concurrency-examples
    
  3. Change your path to the scala-concurrency-examples directory:
    $ cd scala-concurrency-examples
    
  4. Create a single source code directory for our examples:
    $ mkdir src/main/scala/org/learningconcurrency/
    
  5. Now, use your editor to create a build definition file, named build.sbt. This file defines various project properties. Create it in the root directory of the project (scala-concurrency-examples). Add the following contents to the build definition file (note that the empty lines are mandatory):
    name := "concurrency-examples"
    
    version := "1.0"
    
    scalaVersion := "2.11.1"
  6. Finally, go back to the terminal, and run SBT from the root directory of the project:
    $ sbt
    
  7. SBT will start an interactive shell, which we will use to give SBT various build commands.

Now, you can start writing Scala programs. Open your editor, and create a source code file named HelloWorld.scala in the src/main/scala/org/learningconcurrency directory. Add the following contents to the HelloWorld.scala file:

package org.learningconcurrency

object HelloWorld extends App {
  println("Hello, world!")
}

Now, go back to the terminal window with the SBT interactive shell, and run the program with the following command:

> run

Running this program should give the following output:

Hello, world!

These steps are sufficient to run most of the examples in this book. Occasionally, we will rely on external libraries when running the examples. These libraries are resolved automatically by SBT from standard software repositories. For some libraries, we will need to specify additional software repositories, so we add the following lines to our build.sbt file:

resolvers ++= Seq(
  "Sonatype OSS Snapshots" at
    "https://oss.sonatype.org/content/repositories/snapshots",
  "Sonatype OSS Releases" at
    "https://oss.sonatype.org/content/repositories/releases",
  "Typesafe Repository" at
    "http://repo.typesafe.com/typesafe/releases/"
)

Now that we have added all the necessary software repositories, we can add some concrete libraries. By adding the following line to the build.sbt file, we obtain access to the Apache Commons IO library:

libraryDependencies += "commons-io" % "commons-io" % "2.4"

After changing the build.sbt file, it is necessary to reload any running SBT instances. In the SBT interactive shell, we need to enter the following command:

> reload

This enables SBT to detect any changes in the build definition file, and download additional software packages when necessary.

Different Scala libraries live in different namespaces, called packages. To obtain access to the contents of a specific package, we use the import statement. When we use a specific concurrency library in an example for the first time, we will always show the necessary set of import statements. On subsequent uses of the same library, we will not repeat the same import statements.

Similarly, we avoid adding package declarations in the code examples to keep them short. Instead, we assume that the code in a specific chapter is in the similarly named package. For example, all the code belonging to Chapter 2, Concurrency on the JVM and the Java Memory Model, resides in the org.learningconcurrency.ch2 package. Source code files for the examples presented in that chapter begin with the following code:

package org.learningconcurrency
package ch2

Finally, this book deals with concurrency and asynchronous execution. Many of the examples start a concurrent computation that continues executing after the main execution stops. To make sure that these concurrent computations always complete, we will run most of the examples in the same JVM instance as SBT itself. We add the following line to our build.sbt file:

fork := false

In the examples, where running in a separate JVM process is required, we will point this out and give clear instructions.

Using Eclipse, IntelliJ IDEA, or another IDE

An advantage of using an Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA is that you can write, compile, and run your Scala programs automatically. In this case, there is no need to install SBT, as described in the previous section. While we advise that you run the examples using SBT, you can alternatively use an IDE.

There is an important caveat when running the examples in this book using an IDE: editors such as Eclipse and IntelliJ IDEA run the program inside a separate JVM process. As mentioned in the previous section, certain concurrent computations continue executing after the main execution stops. To make sure that they always complete, you will sometimes need to add the sleep statements at the end of the main execution, which slow down the main execution. In most of the examples in this book, the sleep statements are already added for you, but in some programs you might have to add them yourself.

lock icon The rest of the chapter is locked
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.
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 €18.99/month. Cancel anytime