Multithreading - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Multithreading

Description:

two or more threads access a shared variable with no synchronization, and ... Processor then switched to any threads of equal priority ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 44
Provided by: alemanbo
Category:

less

Transcript and Presenter's Notes

Title: Multithreading


1
Multithreading
Outline 1. Concepts 2. Thread Methods in
Java 3. Life Cycle of a Thread 4. Thread
Priorities and Thread Scheduling 5. Thread
Synchronization 6. Producer/Consumer Relationship
with Thread Synchronization 7. Daemon
Threads 8. Runnable Interface 9. Thread Groups
2
1. Processes
  • Consists of
  • Thread(s)
  • Program counter (PC)
  • register set
  • stack
  • Address Space
  • code
  • main memory (data)
  • open files
  • can be shared by multiple threads

3
1. Processes (overview)
4
1.a Threads and Address Space in current OSs
  • One address space, one thread/address space
  • MS-DOS
  • One address space, many threads/address space
  • Pilot
  • Many address spaces, one thread/address space
  • original UNIX
  • Many address spaces, many threads/address space
  • Solaris, Windows NT

5
1.b Thread States
  • Ready
  • eligible to run, but another thread is running
  • Running
  • using the CPU
  • Blocked
  • waiting for something to happen

6
1.b Thread States
7
1.c Context Switching
  • Internal Events (voluntary)
  • ex. disk request, wait for another thread
  • External Events (involuntary)
  • ex. timer
  • Scheduler chooses new thread
  • Exact state of switched thread is saved in Thread
    Control Block

8
1.d Thread Concurrency
  • Race Condition
  • when output depends on ordering of thread
    execution
  • more formally
  • two or more threads access a shared variable with
    no synchronization, and
  • at least one of the threads writes to the
    variable
  • Atomic Operation
  • an operation that, once started, runs to
    completion
  • indivisible

9
1.e Critical Section
  • Section of code that
  • must be executed by one thread at a time
  • if more than one thread executes at a time, have
    a race condition
  • ex. Non-CSR schedule
  • Must have solution that guarantees
  • Mutual exclusion
  • correctness
  • Absence of deadlock/Absence of unnecessary delay
  • no hangup
  • Eventual entry
  • fairness

10
1.f- Structure of Threads for Critical Section
Problem
  • while (true)
  • non-critical section
  • call enter
  • execute CS
  • call exit
  • non-critical section

11
1.g- Locks
  • Two Operations
  • -Acquire (get it, if cant go to sleep)
  • -Release (give it up, possibly wake up a
    thread
  • waiting on it)
  • Lock is shared among all threads

12
1.h- Lock Implementationlock class has queue,
valueInitially queue is empty, value is free
  • Acquire(lock)
  • Disable interrupts
  • if (lock.valuebusy)
  • enQ(lock.queue,thread)
  • go to sleep
  • else
  • lock.valuebusy
  • Enable interrupts
  • Release(lock)
  • Disable interrupts
  • if notEmpty(lock.queue)
  • threaddeQ(lock.queue)
  • enQ(readyList, thread)
  • else
  • lock.valuefree
  • Enable interrupts

13
1.i- Problems with Locks
  • Not general
  • -Only solve simple critical section problem
  • Condition synchronization
  • -Need to wait until some condition is true
  • -Example bounded buffer problem

14
1.j- Bounded Buffer Problem
  • Two threads one producer, one consumer
  • Buffer size of n
  • Locks are difficult to use here
  • -Example producer grabs lock, but must
    release it if
  • buffer if full
  • -Example producer and consumer can access
    distinct
  • location concurrently

15
1.k- Semaphores (Dijkstra)
  • Semaphore is an object containing a value and two
    operations
  • Semaphore value must be nonnegative
  • P operation (atomic)
  • -If value is 0, block else value--
  • V operation (atomic)
  • -If thread blocked, wake up else value
  • Semaphores are resource counters

16
1.l- Critical Sections with Semaphores
  • Semaphore mutex1
  • Entry()
  • P(mutex)
  • Exit()
  • V(mutex)

17
1.m- Bounded Buffer Problemchar bufn, int
front0, rear0semaphore emptyn, full0
  • Producer()
  • while(true)
  • produce message m
  • P(empty)
  • bufrearm
  • rearrear1
  • V(full)
  • Consumer()
  • while(true)
  • P(full)
  • mbuffront
  • frontfront1
  • V(empty)
  • consume m

18
1.n- Problems with Semaphores
  • Used for 2 independent purposes
  • -Mutual exclusion
  • -Condition synchronization
  • Small mistakes may lead to deadlock
  • May want to separate mutual exclusion, condition
    synchronization

19
1.o-Monitors (Hoare)
  • Consists of variables and procedures, like
    classes
  • three key differences from a regular class
  • -only one thread in monitor at a time(mutual
    exclusion
  • is automatic)
  • -special type of variable condition variable
  • -four special operations on condition
    variables wait, signal, broadcast, notempty

20
1.p- Wait, Signal, Broadcast
  • Given a condition variable cond
  • cond-gtWait()
  • -thread is put on queue for cond, goes to
    sleep
  • cond-gtSignal()
  • -if queue for cond not empty, wake up one
    thread
  • cond-gtBroadcast()
  • -wake up all threads waiting on queue for
    cond

21
1.q- Monitor Solution to CS
  • Just make the CS a monitor routine

22
2. Thread Methods in Java
  • Portability
  • Differences between Solaris and Win32 (Windows95,
    NT, etc.)
  • On Solaris
  • A thread runs to completion or until a higher
    priority thread becomes ready
  • Preemption occurs (processor is given to the
    higher-priority thread)
  • On Win32
  • Threads are timesliced
  • Thread given quantum of time to execute
  • Processor then switched to any threads of equal
    priority
  • Preemption occurs with higher and equal priority
    threads

23
2. Thread Methods in Java
  • Thread-related methods
  • See API for more details (especially exceptions)
  • Constructors
  • Thread( threadName )
  • Thread()
  • Creates an auto numbered Thread of format
    Thread-1, Thread-2...
  • run
  • "Does work" of thread
  • Can be overridden in subclass of Thread or in
    Runnable object
  • start
  • Launches thread, then returns to caller
  • Calls run
  • Error to call start twice for same thread

24
2. Thread Methods in Java
  • Thread methods
  • static method sleep( milliseconds )
  • Thread sleeps (does not contend for processor)
    for number of milliseconds
  • Can give lower priority threads a chance to run
  • interrupt
  • Interrupts a thread
  • static method interrupted
  • Returns true if current thread interrupted
  • isInterrupted
  • Determines if a thread is interrupted
  • isAlive
  • Returns true if start called and thread not dead
    (run has not completed)

25
2. Thread Methods in Java
  • Thread methods
  • setName( threadName )
  • getName
  • toString
  • Returns thread name, priority, and ThreadGroup
  • static method currentThread
  • Returns reference to currently executing thread
  • join
  • Calling thread waits for thread receiving message
    to die before it can proceed
  • No argument or 0 millisecond argument means
    thread will wait indefinitely
  • Can lead to deadlock/indefinite postponement

26
3. Life Cycle of a Thread
indicates - Quantum Expiration - yield() -
interrupt()
Born
start()
Sleep Interval Expires
Ready
I/O Completion
Dispatch
notify() or notifyAll()

Running
Issue I/O
Complete
wait()
sleep()
Waiting
Sleeping
Dead
Blocked
27
4. Thread Priorities and Thread Scheduling
  • All Java applets / applications are multithreaded
  • Threads have priority from 1 to 10
  • Thread.MIN_PRIORITY - 1
  • Thread.NORM_PRIORITY - 5 (default)
  • Thread.MAX_PRIORITY - 10
  • New threads inherit priority of thread that
    created it

28
4. Thread Priorities and Thread Scheduling
  • Java scheduler
  • Keeps highest-priority thread running at all
    times
  • If timeslicing available, ensure equal priority
    threads execute in round-robin fashion
  • New high priority threads could postpone
    execution of lower priority threads
  • Indefinite postponement (starvation)
  • Priority methods
  • setPriority( int priorityNumber )
  • getPriority
  • yield - thread yields processor to threads of
    equal priority
  • Useful for non-timesliced systems, where threads
    run to completion

29
4. Thread Priorities and Thread Scheduling
Priority 10
Priority 9
Priority 8
Priority 7
Priority 6
Priority 5
Priority 4
Priority 3
Priority 2
Priority 1
30
4. Thread Priorities and Thread Scheduling
  • Example program
  • Create a class derived from Thread
  • Use sleep method
  • Overview
  • Create four threads, which sleep for random
    amount of time
  • After they finish sleeping, print their name
  • Program has two classes
  • PrintThread
  • Derives from Thread
  • Instance variable sleepTime
  • ThreadTester
  • Creates four PrintThread objects

31
  • Class ThreadTester
  • 1. main
  • 1.1 Initialize objects
  • 1.2 start
  • ---------------
  • Class PrintThread
  • 1. extends Thread
  • 1.1 Instance variable

32
  • 1.2 Constructor
  • 1.2.1 Randomize sleepTime
  • 2. run
  • 2.1 sleep

33
5. Thread Synchronization
  • Monitors
  • Object with synchronized methods
  • Any object can be a monitor
  • Methods declared synchronized
  • public synchronized int myMethod( int x )
  • Only one thread can execute a synchronized method
    at a time
  • Obtaining the lock and locking an object
  • If multiple synchronized methods, only one may be
    active

34
5. Thread Synchronization
  • Thread may decide it cannot proceed
  • May voluntarily call wait while accessing a
    synchronized method
  • it is removed from contention for monitor object
    and processor
  • Thread goes to waiting state
  • Other threads try to enter monitor object
  • Can call notify to tell a single waiting thread
    to enter ready state
  • notifyAll - tells all waiting threads to enter
    ready state

35
6. Producer/Consumer Relationship with Thread
Synchronization
  • Condition variable of a monitor
  • Variable used to test some condition
  • Determines if thread should call wait
  • For our producer / consumer relationship
  • Condition variable determines whether the
    producer should write to buffer or if consumer
    should read from buffer
  • Use boolean variable writeable
  • If writeable true, producer can write to buffer
  • If false, then producer calls wait, and awaits
    notify
  • If writeable false, consumer can read from buffer
  • If true, consumer calls wait

36
6. Producer/Consumer Relationship with Thread
Synchronization
  • Example program
  • Synchronize the set and get methods
  • Once the producer writes to memory, writeable is
    false (cannot write again)
  • Once consumer reads, writeable is true (cannot
    read again)
  • Each thread relies on the other to toggle
    writeable and call notify
  • class HoldIntegerSynchronized
  • contains synchronized set and get methods

37
  • -------------------------
  • Class HoldInteger Synchronized
  • 1. Instance variables
  • 2. setSharedInt (synchronized)

38
  • 3. getSharedInt (synchronized)

39
7. Daemon Threads
  • Daemon threads
  • Threads that run for benefit of other threads
  • Garbage collector
  • Run in background
  • Use processor time that would otherwise go to
    waste
  • Unlike normal threads, do not prevent a program
    from terminating
  • When only daemon threads remain, program exits
  • Must designate a thread as daemon before start
    called
  • setDaemon( true )
  • Method isDaemon
  • Returns true if thread is a daemon thread

40
8. Runnable Interface
  • How to do Multithreading for an already derived
    class ?
  • Implement interface Runnable
  • New class objects "are" Runnable objects
  • Override run method
  • Controls thread, just as deriving from Thread
    class
  • In fact, class Thread implements interface
    Runnable
  • Create new threads using Thread constructors
  • Thread( runnableObject )
  • Thread( runnableObject, threadName )

41
8. Runnable Interface
  • Synchronized blocks of code
  • synchronized( monitorObject ) ...
  • monitorObject- Object to be locked while thread
    executes block of code
  • Suspending threads
  • In earlier versions of Java, there were methods
    to suspend/resume threads
  • Dangerous, can lead to deadlock
  • Instead, use wait and notify

42
9. Thread Groups
  • Thread groups
  • Threads in a thread group can be dealt with as a
    group
  • May want to interrupt all threads in a group
  • Thread group can be parent to a child thread
    group
  • Class ThreadGroup
  • Constructors
  • ThreadGroup( threadGroupName )
  • ThreadGroup( parentThreadGroup, name )
  • Creates child ThreadGroup named name

43
9. Thread Groups
  • Associating Threads with ThreadGroups
  • Use constructors
  • Thread( threadGroup, threadName )
  • Thread( threadGroup, runnableObject )
  • Invokes run method of runnableObject when thread
    executes
  • Thread( threadGroup, runnableObject,
    threadName )
  • As above, but Thread named threadName
Write a Comment
User Comments (0)
About PowerShow.com