# Concurrent Programming

**Source: MIT** [**6.005 — Software Construction**](http://stellar.mit.edu/S/course/6/fa14/6.005/)&#x20;

<http://web.mit.edu/6.005/www/fa14/classes/17-concurrency/>

## Concurrency <a href="#concurrency" id="concurrency"></a>

**Concurrency** means multiple computations are happening at the same time. Concurrency is everywhere in modern programming, whether we like it or not:

* Multiple computers in a network
* Multiple applications running on one computer
* Multiple processors in a computer (today, often multiple processor cores on a single chip)

In fact, concurrency is essential in modern programming:

* Web sites must handle multiple simultaneous users.
* Mobile apps need to do some of their processing on servers (“in the cloud”).
* Graphical user interfaces almost always require background work that does not interrupt the user. For example, Eclipse compiles your Java code while you’re still editing it.

## Two Models for Concurrent Programming <a href="#two_models_for_concurrent_programming" id="two_models_for_concurrent_programming"></a>

There are two common models for concurrent programming:*shared memory\_and\_message passing*.

![shared memory](http://web.mit.edu/6.005/www/fa14/classes/17-concurrency/figures/shared-memory.png)

**Shared memory.**&#x49;n the shared memory model of concurrency, concurrent modules interact by reading and writing shared objects in memory.

Other examples of the shared-memory model:

* A and B might be two processors (or processor cores) in the same computer, sharing the same physical memory.
* A and B might be two programs running on the same computer, sharing a common filesystem with files they can read and write.
* A and B might be two threads in the same Java program (we’ll explain what a thread is below), sharing the same Java objects.

![message passing](http://web.mit.edu/6.005/www/fa14/classes/17-concurrency/figures/message-passing.png)

**Message passing.**&#x49;n the message-passing model, concurrent modules interact by sending messages to each other through a communication channel. Modules send off messages, and incoming messages to each module are queued up for handling. Examples include:

* A and B might be two computers in a network, communicating by network connections.
* A and B might be a web browser and a web server – A opens a connection to B, asks for a web page, and B sends the web page data back to A.
* A and B might be an instant messaging client and server.
* A and B might be two programs running on the same computer whose input and output have been connected by a pipe, like`ls | grep`typed into a command prompt.

## More to Read: <http://web.mit.edu/6.005/www/fa14/classes/17-concurrency/>
