definitions n.
Skip this Video
Loading SlideShow in 5 Seconds..
Definitions PowerPoint Presentation
Download Presentation


0 Views Download Presentation
Download Presentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Definitions • Concurrent program – Program that executes multiple instructions at the same time. • Process– An executing program (the running JVM for Java programs) • Thread – A sequence of executing instructions. A single process can concurrently execute many threads. • Critical Section – A resource or area of code that must be protected from simultaneous access by multiple threads. • Mutual Exclusion – The mechanism for protecting a critical section. • Monitor – A language construct in Java that utilizes the synchronized reserved word to enforce mutual exclusion.

  2. Examples of Concurrency • JVM • Garbage Collection reclaiming memory of unused objects • GUI Listeners that respond to user or program events Note: In these examples, each thread is executing instructions at the same time.

  3. Implementing a thread in Java • Signature line: There are two alternatives: • Create a class that extends Thread. • Add implements Runnable to an existing class line . • Implement a run method with the following signature: public void run() • Reference and instantiate the thread • Thread thread = new Thread(this) • Thread thread = new Thread(new RunnableClass()); • Start the thread (thread.start()). • The thread ends when it returns from the run method.

  4. Thread Example See: demos/, demos/ThreadRunnableDemo Sample Output: 1111222334445551223334455 Note: The order of execution depends on when threads are scheduled to run

  5. Critical Sections and Mutual Exclusion • Critical Section:A sequence of instructions that once started by a thread must be completed by that thread before some other thread starts into the sequence. • Mutual Exclusion:A technique that allows only one thread at a time into a critical section. • The Producer/Consumer and Dining Philosopher problems are classical computer science problems used to illustrate critical sections and mutual exclusion • Our text has a producer consumer example • Lab 7 implements the dining philosopher problem See demos/ThreadSharedData*.java

  6. Monitors A mutual exclusion technique built into the language syntax • Java • Every object that may be written by more than one thread needs a monitor (roughly analogous to a key to a lock). • Keyword synchronized creates a monitor. • Only one thread at a time can enter a synchronized block • A monitor ‘locks’ a particular object while a block of code executes. The monitor ensures no thread enters the block if another thread is executing instructions in that block. • Example • private synchronized int add(int a) { int ret = this.x; this.x = this.x + a; return ret;} • Guarantees a thread will be able to read x, add a to x, store x, and return the value read without any other thread intervening. • If this.x is 3, without synchronization, t[hread]0 could read 3, t1 read 3, add 2, store and return 5, then t0 add 1 (to 3) and store and return 4. Note the sum would be 4, but it should be 6.

  7. Wait Ready Sleep Run Dead Thread States • Ready: A thread is ready to execute • Run: The thread is executing. • Wait: A thread cannot execute until the monitor it is waiting on receives a call to notify() or notifyAll().. • Sleep: thread called sleep(). It must be interrupted (by another thread or by its sleep timer running out) in order to continue. • Dead: run method finished State Diagram

  8. Object Class Monitor Methods • wait() – Move this thread to the wait state • notify() – Move one waiting thread from the wait to ready state. The programmer has no control over which thread gets chosen • notifyAll() – Move all waiting threads from the wait to the ready state.

  9. Other Thread Methods The word ‘thread’ refers to one of two things: • A running set of instructions or • A java object whose class implements Runnable It is handy to think of the Runnable object as ‘running’ the instructions. • Give up control: sleep(long milliseconds), • Wait for a started thread to die: join(), join(long milliseconds) • Get current thread object:Thread.currentThread() • Check to see if (usually the current) thread has been interrupted:isInterrupted() • Name of thread: getName(), setName(String name) • Retrieve or set a thread’s Priority: getPriority(), setPriority() • See if a thread is alive:isAlive()

  10. Think Hungry Eat Famished Starved State Diagram P1 P0 C0 Lab Assignment C1 C4 P2 C2 P4 C3 • Dining Philosopher Problem • Five philosophers • Requires two chopsticks to eat • Alternates between eat, think, hungry, famished, and starve • Never puts down a chopstick until finishes eating P3 Implementation hints Instantiate philosopher array of five threads Instantiate array of five chopsticks

  11. Dining Philosopher Issues: • Deadlock: The application cannot continue because it gets into a state where resources cannot be released or obtained • Starvation: Some threads never get a chance to execute • Critical Sections: The program must limit the number of threads executing a part of an algorithm