">Slide show

Threads and Java

Ogg-Vorbis format, 52Mbytes WAV format, 290Mbytes

Distributed systems and concurrent systems

Concurrency

Unix processes

Scheduling policies

Threads

Shared memory problems

Threads

Thread scheduling

Thread creation

Two methods

Example


Sequential server

while (msg = getMessage())
    handle msg

Concurrent server

while (msg = getMessage())
    create new thread to handle msg

Thread life-cycle

Thread alive states

Example broken shared memory

Mutual exclusion

Use of synchronized

Fixing bank account

More synchronization

Synchronization on a shared object

Fine control of threads

There are a number of ways in which threads can control execution

Wait/notify

Reader/Writer example

Deadlock

Example:

Two processes wish to communicate with each other, in a 2-way manner. Two pipes can be used for this:

The two processes issue read/write requests P1: P2: read(pipe2) read(pipe1) write(pipe1) write(pipe2) Deadlock occurs.

Example:

When the "give way to the right" rule was in force, this was a common situation:

Deadlock detection

A resource allocation graph is a graph showing processes, resources and the connections between them. Processes are modelled by circles, resources by squares. If a process controls a resource then there is an arrow from resource to process. If a process requests a resource an arrow is shown the other way.

For example, P1 is using R1 and has requested R2, while P2 is using R2.

Deadlock can now occur if P2 requests R1 - setting up a cycle.

The total set of conditions for deadlock to occur are:

  1. Mutual exclusion. Each resource is either currently assigned to one process or is available.
  2. Hold and wait. Processes holding resources can request new ones.
  3. No preemption. Resources granted cannot be taken away, but must be released by the process holding them.
  4. Circular wait. There must be a cycle in the resource allocation graph.

Deadlock and the bank account

Deadlock prevention

To prevent deadlocks from occurring, one of the four conditions must be disallowed.

  1. Mutual exclusion. Make some resources unsharable, such as printers, tape drives.
  2. Hold and wait. Process must request all needed resources at one time. This results in wasted resources.

    Process must release all current resources before requesting more. This is not feasible.

  3. No Preemption. Make it possible for the O/S to make a process give up a resource. This may result in lost data.
  4. Circular wait. Make it impossible for cycles to occur in the graph by restricting the types of graphs that can be made. For example, give each resource a priority number. If a resource is held with a priority, then a new resource can only be requested of lower priority.

Bank account avoiding circular wait

Deadlock avoidance

Deadlock prevention is to ensure that deadlocks never occur. Deadlock avoidance is attempting to ensure that resources are never allocated in a way that might cause deadlock.

There are situations that may give rise to cycles, but in practise will not lead to deadlock. For example, P1 and P2 both want R1 and R2, but in this manner:


P1 can be given R1 and P2 can be given R2. P1 will require R2 before it can give up R1, but that is ok - P2 will give up R2 before it needs R1.

The Banker's algorithm can be used to ensure this, but it is too complicated for general use.

Listing a directory (and subdirectories)

Listing all the files in a directory is a recursive operation:

This can be done synchronously using one thread or concurrently using one thread per directory

Recursive single-thread solution

Multi-threaded solution

Each directory could be handled by a separate thread. When a list of files is found for each directory, it's name is printed if it is an ordinary file, but if it is a directory then a new thread is created to handle it.

Summing the size of files in directories

Listing files can be made more complex by finding their size and summing them.

Recursive sum

Multi-threaded sum

This one is a lot more complicated!

Conclusion


Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Mon Aug 15 12:12:36 EST 2005
Copyright ©Jan Newmarch