👨‍💻
Software Engineering
Clean Code: Agile Software Craftsmanship
Clean Code: Agile Software Craftsmanship
  • Clean Code
  • 1: Clean Code
    • There Will Be Code
    • Bad Code
    • The Total Cost of Owning a Mess
    • Schools of Thought
    • We are Authors
    • The Boy Scout Rule
    • Prequel and Principles
    • Conclusion
  • 2: Meaningful Names
  • 3: Functions
  • 4: Comments
  • 5: Formatting
  • 6: Objects and Data Structures
  • 7: Error Handling
  • 8: Boundaries
  • 9: Unit tests
  • 10: Classes
  • 12: Emergence
  • 13: Concurrency
    • Why Concurrency?
    • Challenges
    • Concurrency Defense Principles
    • Know Your Library
    • Know Your Execution Models
    • Beware Dependencies between Synchronized Methods
    • Keep Synchronized Sections Small
    • Writing Correct Shut-Down Code is Hard
    • Testing Threaded Code
    • Conclusion
Powered by GitBook
On this page
  • Basic Definitions
  • Producer-Consumer Problem
  • Readers-Writers Problem
  • Dining Philosophers Problem
  • Summary
  1. 13: Concurrency

Know Your Execution Models

There are several way to partition behavior in a concurrent application

PreviousKnow Your LibraryNextBeware Dependencies between Synchronized Methods

Last updated 9 months ago

Basic Definitions

Bound Resources

Resources of a fixed size or number used in a concurrent environment. (e.g. DB connections)

Mutual Exclusion

One one thread can access shared data or a shared resource at a time.

Starvation

One thread or group of threads is prohibited from proceeding for an excessively long time (or forever).

Deadlock

Two or more threads waiting for each other to finish. Each thread has a thread the other requires. Neither can finish.

Livelock

Threads in lockstep, each trying to do work but the other is in the way. Due to resonance, threads continue trying to make progress but are unable for an excessively long time - or forever.

Producer-Consumer Problem

  • One or more producer threads create some work and place it in a buffer or queue.

  • One (or more) consumer threads acquire that work from the queue and complete it.

  • The queue between them is a bound resource. Both wait to be notified when they can continue.

Readers-Writers Problem

When you have a shared resource that serves as a source of information for readers, but is occasionally updated by writers, throughput is an issue.

Emphasizing throughput can cause starvation.

Tough Balancing Act: Coordinating readers to avoid reading something being updated by a writer.

Challenge: Balance the needs of reads and writes to:

  • Satisfy correct operation

  • Provide reasonable throughput.

  • Avoid starvation.

Dining Philosophers Problem

An illustration to demonstrate concurrent algorithm design and solutions:

Philosophers sitting at a circular dining table, each with a fork to their left. A bowl of spaghetti in the center of the table. They spend their time thinking unless they get hungry. They can only eat if they have two forks. Once a philosopher eats, he puts both forks down making them available to the philosophers on either the right or left.

  • Philosophers = Threads.

  • Forks = resources.

Unless carefully designed, systems that compete in this way can experience deadlock, livelock, throughput, and efficiency degradation.

Summary

Most concurrent problems will be some variation of these problems.

Study and write solutions to these so that you're prepared.

Dining philosophers problemWikipedia
Producer–consumer problemWikipedia
Logo
Logo
Readers–writers problemWikipedia
Logo