Java Concurrency - Part 1: Introduction

Milky Way

This article is a part of the Java Programming Language article series.

  • Performing multiple actions at the same time
  • Listening to music, watching movies: streaming audio and video, listening and watching
  • Browsing websites while downloading files in the web browser
  • Processing time for a single core is shared among processes and threads through an OS feature called time slicing
  • A runtime environment with its own memory space
  • An application can consist of one or more processes
  • Inter Process Communication (IPC) resources are used to facilitate communication between processes in operating systems
  • IPC provides communication on the same computer or between different computers
  • Socket programming
  • Lightweight processes
  • Each process has at least one thread
  • Threads share the resources of the process
  • System threads: memory management, signal handling
  • Program startup thread: main thread
  • Run asynchronous tasks
  • Priority can be assigned to threads
  • Thread class
  • start(): Starts the thread
  • Thread.sleep(): Pauses execution
  • interrupt(): Stops the thread
  • Thread.interrupted(): Checks if thread is interrupted, throw an InterruptedException
  • join(): Allows one thread to wait for the completion of another, dependent on the OS for timing
  • isAlive(): Checks if thead is alive
  • Thread.currentThread().getName(): Gets current thread name

Option 1: Implement Runnable interface

Option 2: Extend Thread class

  • Threads communicate by sharing access to fields
  • Errors: Thread Interference, Memory Consistency Errors
  • Solution: Synchronization

Causes

  • Thread contention: Starvation and livelock
  • Two or more threads try to access the same resource simultaneously
  • One or more threads are executed more slowly or suspended

Use

  • Synchronized Methods
  • Synchronized Statements
  • Implicit Locks and Synchronization
  • Atomic Access
  • Acting on the same data with multiple operations running in different threads

c++ expression

  1. Retrieve the current value of c
  2. Increment the retrieved value by 1
  3. Store the incremented value back in c

Case

  • Thread A invokes increment
  • Thread B invokes decrement
  • The initial value of c is 0

Possibility 1

  1. Thread A: Retrieve c
  2. Thread B: Retrieve c
  3. Thread A: Increment retrieved value; result is 1
  4. Thread B: Decrement retrieved value; result is -1
  5. Thread A: Store result in c; c is now 1
  6. Thread B: Store result in c; c is now -1

Thread interference bugs can be difficult to detect and fix

  • Inconsistent views of the same data with different threads

Case

  1. Thread A increments counter
  2. Thread B prints out counter

Possibility 1

  • The value printed out might well be 0

Solution

  • The happens-before relationship: guarantees memory writes between different threads
  • Constructors cannot be synchronized
  • Using the synchronized keyword with a constructor is a syntax error
  • Causes liveness problems
  • Specify the object that provides the lock
  • Happens all at once
  • Cannot stop in the middle
  • No side effects are visible until it is complete
  • Reads and writes are atomic for reference variables and primitive variables (except long and double)
  • Declare volatile: all variables
  • Ability to execute in a timely manner
  • Deadlock
  • Starvation
  • Livelock
  • A situation where two or more threads are blocked forever, waiting for each other
  • Both threads will block when they attempt to invoke bowBack
  • Neither block will ever end, because each thread is waiting for the other to exit bow
  • A situation where a thread is unable to gain regular access to shared resources and is unable to make progress

Case

  • An object provides a synchronized method that often takes a long time to return
  • If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked
  • A thread often acts in response to the action of another thread
  • If the other thread’s action is also a response to the action of another thread, then livelock may result

Case

  • Two people attempting to pass each other in a corridor

--

--

https://gokhankanber.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store