Concurrency - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Concurrency

Description:

Two tasks both mixing processing ... Multiple processors tackling the same problem. Pipelining ... Little green gremlins? Priority Inversion: Mars Pathfinder ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 22
Provided by: chris520
Category:

less

Transcript and Presenter's Notes

Title: Concurrency


1
Concurrency
  • Outline potential applications of concurrency
  • Explain typical problems with concurrency
  • Use appropriate vocabulary

2
Example 1 Responsiveness
  • Maintaining a responsive user interface
  • While communicating
  • While performing complex processing

3
Example 2 Multiple External Actors
  • Bank
  • Distributed system
  • Simultaneous activity
  • Components interact with body of information
  • Must maintain consistency

4
Example 3 Efficiency
  • Making use of idle time
  • Two tasks both mixing processing I/O
  • Sequential one after another
  • Parallel using processor when awaiting I/O

Task 1
Task 2
Reduces total time average waiting time
5
Example 4 Power / Speed
  • Parallel Systems
  • Multiple processors tackling the same problem
  • Pipelining
  • Each processor carries out a step of the process
  • Division of labour
  • Each processor works on part of the data

6
Are Concurrent Programs Needed?
  • The world is concurrent
  • Programs must handle simultaneous activities
  • Cant predict order of requests
  • Cant write a sequential program
  • Processor speed is limited
  • Parallelism is only way to increase speed

7
Handling Concurrency
  • Threads
  • Simultaneously executing components
  • Use one thread to manage an external actor
  • Event Handlers
  • Blocks of code triggered by an event
  • Keep event queue handle events sequentially

Event
Event handler
Event handler
Event Queue
Event handler
8
Concurrent Components
  • Other terms
  • Activity
  • Task
  • Fiber
  • Co-routine
  • Application
  • Complete program
  • OS may run several
  • Process
  • Code Data State of execution
  • Heavyweight
  • Has own memory space
  • Thread
  • Code Data State of execution
  • Lightweight
  • Shares memory space with other threads

Processes
Thread-like, co-operatively scheduled
9
Parallel Concurrency?
  • Parallel Systems
  • Multiple processors working simultaneously
  • Concurrent Systems
  • Executing multiple programs at the same time
  • May see progress on several programs by sampling

Time-slicing
Concurrent
Parallel
Multi-processor
10
Parallel ? Concurrency does it matter?
  • No!
  • Design all concurrent programs like parallel ones
  • You cannot tell when the processor will switch
  • Any thread can be interrupted part way through
  • Even on a single processor system
  • Otherwise, your program will have bugs
  • And they will be difficult to find
  • Intermittent
  • Time-dependent
  • Hard to reproduce

NO Treat all programs as parallel
11
Java Concurrency
  • Threads built into the language
  • Portability
  • Synchronisation primitives built in to objects
  • Each object has a lock
  • Well-behaved threads seize lock before
    manipulating the object
  • Ensures mutual exclusion
  • Low-level can be misused
  • No protection against deadlock
  • Java 2 provides additional primitives

12
Java Threads
  • A class with a run() method that
  • extends (inherits from) Thread
  • Activated by the start() method
  • Creates a new thread,
  • Executes run() in parallel
  • Alternatively a class that
  • implements Runnable (an interface)
  • Provides a run() method
  • Used to create a thread
  • Thread myThread new Thread(myRunnable)
  • myThread.start()

13
Example
  • public class NumberThread extends Thread
  • int num
  • public NumberThread(int num) this.num
    num
  • public static void main(String args)
  • NumberThread numberThread new
    NumberThread5
  • for(int i0 i lt 5 i)
  • numberThreadi new
    NumberThread(i)
  • numberThreadi.start()
  • static void display(int val)
  • for (int rows 0 rows lt 4 rows)
  • for (int i 0 i lt 35 i)
  • System.out.print(val)
  • System.out.println()

14
Unsynchronized Output
  • Interleaving of output
  • Results vary at random
  • Need mutually exclusive access to screen

15
Locks
  • The first task to seize a lock can proceed
  • Later tasks seeking the lock are suspended
  • Until the lock is released

16
Example
  • public class NumberThread extends Thread
  • int num
  • public NumberThread(int num) this.num
    num
  • public static void main(String args)
  • NumberThread numberThread new
    NumberThread5
  • for(int i0 i lt 5 i)
  • numberThreadi new
    NumberThread(i)
  • numberThreadi.start()
  • synchronized static void display(int val)
  • for (int rows 0 rows lt 4 rows)
  • for (int i 0 i lt 35 i)
  • System.out.print(val)
  • System.out.println()

Only one thread at a time can execute this
method
17
Synchronized Output
18
Priority Inversion Mars Pathfinder
  • Pathfinder had an "information bus,
  • a shared memory area for data exchange
  • Bus access synchronized by a lock
  • mutual exclusion lock (mutex).
  • Threads have priorities reflecting their urgency.
  • There were occasional problems
  • Little green gremlins?

19
Priority Inversion Mars Pathfinder
  • A low priority weather thread ran infrequently
  • acquire mutex, write to the bus, release mutex.
  • If the high priority bus management thread ran
    while the mutex was held, it blocked, waiting for
    the weather thread to release the mutex
  • If a medium priority communications thread was
    scheduled during this short time, it prevented
    the weather task running, blocking the management
    task.
  • A watchdog timer would trigger, notice the data
    bus task had not run recently, decide there was a
    problem, reset the system, losing data.
  • Fix give threads holding a mutex the maximum
    priority of waiting threads

Information Bus
Low
mutex
High
Low
Medium
20
Vocabulary
  • Critical Region
  • Sections of code which could cause problems if
    multiple threads simultaneously execute them
  • Synchronisation
  • Co-ordinating threads processes
  • Examples of synchronisation
  • Mutual Exclusion
  • Ensuring at most one thread is in a critical
    region
  • Communication
  • Passing data between threads

21
Summary
  • Concurrency is essential
  • Treat concurrent activities as if parallel
  • Java provides threads
  • Treat concurrent activities as if parallel
  • Threads / Processes
  • Concurrent program parts
  • Threads share memory space
  • Share global variables
  • Consistency problems unless care taken
  • Treat concurrent activities as if parallel
Write a Comment
User Comments (0)
About PowerShow.com