Distributed Programming in Java - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Distributed Programming in Java

Description:

running a single program was inefficient. 2. Processes ... Runnable Example. Countdown thread. Runnable - Example. Runnable Example ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 41
Provided by: protonScs
Category:

less

Transcript and Presenter's Notes

Title: Distributed Programming in Java


1
Distributed Programming in Java
  • Concurrency (1)

2
Computers
Input Devices
CPU
Memory
Output Devices
3
History of Concurrency
  • 1. Single program
  • 2. Processes
  • 3. Threads

4
1. Single program
  • No operating systems
  • Executing Programs from beginning to end
  • advantage
  • direct access to all the resources
  • CPU, memory, IO channels
  • disadvantage
  • running a single program was inefficient

5
2. Processes
  • A process is an operating-system abstraction that
    allows a computer to support many units of
    execution
  • Each process typically represents a running
    program
  • A computer can have multiple running programs
  • Multiple programs share the same resources
  • CPU, Memory, IO Channels

6
Properties of Processes
  • Independently executing programs
  • Isolated, no interference
  • cannot access one anothers storage locations x
    10
  • IPC sockets, pipes, signaling, semaphores, files

7
Advantages
  • Resource utilization
  • Programs may have to wait for I/O, other
    programs can run
  • Fairness
  • multiple users and programs have equal claims on
    computer resources
  • Convenience
  • often easier or more desirable to write
  • several programs, each perform a single
  • task

8
Disadvantages
  • Overhead
  • creating
  • scheduling
  • managing
  • maving program data in and out
  • Less autonomous
  • a machine crash causes by one process kills all
    processes

9
3. Threads
  • Divide processes into threads to lower
  • overhead in terms of autonomy

10
Processes Threads
  • several threads of a process can coexist
  • they share process-wide resources such as memory,
    file handles,
  • but have their own program counter, stack, and
    local variables
  • Multiple threads can be scheduled on multiple CPU
    simultaneously.

11
Modern OS scheduling
  • Threads are lightweight processes
  • OS now treat threads, not processes, as the basic
    units of scheduling
  • Threads execute simultaneously and asynchronously
  • All threads within a process have access to the
    same variables and allocate objects from the same
    heap
  • A thread may modify variables that another thread
    is using, with unpredictable results

12
Benefits
  • Exploit multiple processes 2, 4,
  • Simplify the writing of programs
  • one thread for each kind of task
  • Simplify the handling of asynchronous events
  • thread-per-client for web server, I/O handler
  • More responsive user interfaces
  • does not appears to freeze using separate
    thread

13
Risks
  • 1. Safety hazards
  • 2. Liveness hazards
  • 3. Performance hazards
  • 4. Reusability hazards

14
1. Safety hazards
  • Nothing bad ever happens to an object
  • Public class UnsafeSequence
  • private int value
  • public int getNext()
  • return value

15
  • T1 value-gt9 91 -gt10 value 10
  • T2 value-gt10,..
  • T3 value-gt9 91-gt10 value 10

16
Inconsistent State
  • Withdraw from bank account while it is in the
    midst of a transfer
  • Could overdraw account
  • Could lose money
  • A storage location is read in the midst of being
    written to
  • Could result in reading some old with some new
    bytes (a nonsense value)

17
Solution locks
  • Public class SafeSequence
  • private int value
  • public synchronized int getNext()
  • return value

18
2. Liveness
  • Something eventually happens within an activity
    (runs towards completion)

19
Liveness hazards
  • Deadlocks
  • Starvation
  • livelock

20
Deadlock
  • Circular dependencies among threads

21
3. Performance hazards
  • Extent to which activities execute quickly
  • Concurrency can be expensive
  • Overhead decreases with newer JVMs

22
Overheads
  • Context switches
  • suspend the active thread so another thread can
    run saving and restoring execution context
  • Scheduling instead of running
  • Synchronization
  • Communication (between threads, if using multiple
    CPUs)

23
4. Reusability hazards
  • Utility of object across multiple contexts
  • But ... concurrency adds complexity
  • Behavior is nondeterministic
  • Correctness not (easily) automatically testable
    (transience of faults)
  • Concurrency adds context dependence
  • Components only safe/live in intended context (eg
    they make assumptions about who accesses them,
    affecting safety)

24
Thread Lifecycle
  • Instances of Thread progress through various
    states as shown below

25
Design Space
26
Concurrent Programming
  • Informal definition of concurrency
  • A concurrent program is a program that
  • does more than one thing at a time
  • Simultaneity often simulated
  • A single time-shared CPU switches among
  • the different activities or threads quickly
  • Threads are permitted to proceed in parallel,
    when possible, or by time-sharing

27
Concurrent Programming
  • On one processor, simultaneity is simulated

28
Distributed Programming
  • Distributed programs are mostly concerned with
    breaking down an application into autonomous
    agents (active objects) that can be distributed
    on a network of computers, and their coordination
    through messages
  • Not limited to the boundaries of a process or
    network node no shared memory (ie references in
    the local address space are not valid in a remote
    address space)

29
Distributed Programming
  • Collaboration between active objects

30
Parallel Programming
  • Programmer has control over how to map threads to
    multiple processors
  • Threads communicate via shared memory
  • Goals of parallel programming
  • Increase throughput (ie the number of
    computations per time)
  • Scalability (ie the rate of improvement)
  • Common variant is task-based parallelism threads
    correspond to subtasks

31
Parallel Programming
  • Task decomposition

32
Building Blocks
  • Agents and objects

33
Systems Objects Actitivities
  • In an object-centric view
  • Collection of interconnected objects
  • Not a random soup, but objects cluster in groups
    (a property known as cohesion)
  • In an activity-centric view
  • Collection of possibly concurrent activities
  • Different levels of organization (message sends,
    tasks, transactions, threads, etc.)

34
Design Forces
  • Major forces that must be addressed in the design
    of a concurrent system

35
Threads (Definition)
  • A thread is call sequence that executes
    independently from others

36
Threads in Java
  • Thread objects maintain bookkeeping and control
    for a thread of control

The thread executes instructions from its
run()method. The actual code depends on the
subclass.
class MyThread extends Thread public void
run() // ... // this is not
recommended !!!
37
Runnable
  • Instead, we usually implement run() in a class
    derived from the Runnable interface

38
Runnable Example
  • Countdown thread

39
Runnable - Example
40
Runnable Example
Write a Comment
User Comments (0)
About PowerShow.com