Definitions Process – An executing program

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Java, Java, Java Object Oriented Problem Solving Chapter 13: Threads and Concurrent Programming.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Introduction to Threads Computers can perform many tasks concurrently – download a file, print a file, receive , etc. Sequential languages such.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and Multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Threading and Concurrency COM379T John Murray –
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Java 3: Odds & Ends Advanced Programming Techniques.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading and Garbage Collection Session 16.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Distributed and Parallel Processing George Wells.
Java Thread Programming
Multithreading / Concurrency
Java Multithreading.
Multithreading.
Chapter 5: Process Synchronization – Part 3
Multithreaded Programming in Java
EE 422C Multithreading & Parallel Programming
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
برنامه‌نویسی چندنخی Multi-Thread Programming
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Definitions Process – An executing program Thread – A path of execution through a process. A single process can concurrently execute many threads. Concurrent programming – Code that executes multiple algorithms at a time Critical Section – A resource or area of code that must be protected from unlimited 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 enforces mutual exclusion.

Examples of Concurrency Note: Each thread has its own sequence of instructions JVM Interpreted instructions in a Java application or applet Garbage Collection reclaiming memory of unused objects GUI Listeners that respond to user or program events Operating System Respond to interrupts Schedule execution among processes based on a quantum or priority

Implementing a thread in Java Signature line: There are two alternatives: Add implements Runnable to the JFrame or Applet class signature line . Create a class that extends Thread. Program a run method (public void run() {}) Reference and instantiate the thread Thread thread = new Thread(this, "name"); thread = new Thread(new ThreadClass(), "name"); Start the thread (thread.start()). The thread ends when it returns from the run method.

Thread Example Assumption: round robin scheduling algorithm public class NumberThread extends Thread { private int num; public NumberThread(int num) {this.num=num;} public void run() { for (int k=0; i<5; k++){System.out.println(num);} } public static void main(String[] args) { for (int t=1; t<=5; t++) new NumberThread(t).start(); Sample Output: 1111222334445551223334455 Note: The order of execution depends on operating system scheduling

Other Thread Methods Name of thread: getName(), setName(String name) Thread Priority: getPriority(), setPriority() Give up control: sleep(long milliseconds), yield() Wait for thread to die: join(), join(long milliseconds) Get object of current thread: currentThread() See if thread is alive: isAlive() See if thread interrupted: isInterrupted() Preemption: Higher priority threads will run before lower priorities

Thread Execution Thread that run continuously consume all of the CPU cycles in the computer. Threads should suspend themselves during execution. Example of a run method public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InteruptedException e) { } } }

Critical Sections and Mutual Exclusion Only one synchronized method of a class can execute at a time Critical Section: A group of instructions that will fail if the number of concurrent threads executing it is not limited Mutual Exclusion: The technique that enforces a critical section Examples: semiphores, condition variables, monitors Java uses monitors The Producer/Consumer, Reader/Writer, and Dining Philosopher are classical computer science problems used to illustrate critical sections and mutual exclusion Our text has a producer consumer example We'll briefly discuss the reader/writer problem in class Lab 8 implements the dining philosopher problem

Monitors A mutual exclusion technique built into the language syntax Illustration In a barbershop, people wait for the barber chair those waiting make a mad dash to the chair when it is free No assumptions can be made as to who wins Java Every object is a monitor with a lock flag (i.e. barber chair) Only one thread can have ('acquire') the lock Java uses the synchronized modifier for this purpose. Acquire a lock: synchronized(object) { // critical section } Example private synchronized int add(int a) {x = 3; x += b; return x;} Only one thread can execute an object's synchronized method. Results are unpredictable if we leave off the synchronized modifier

Thread States Ready: A thread is OK to execute Run: The thread is executing. If it's quantum runs out or a higher priority thread becomes ready, Java will switch to another thread if possible Wait: A thread cannot execute until another thread notifies Block: I/O must complete for thread to go to ready Dead: run method finished Sleep: thread called sleep(). It must be interrupted to continue Ready Run Block Sleep Wait Dead State Diagram

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 waitig threads from the wait to the ready state.

Lab Assignment 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 Starve Eat Think Famished Hungry State Diagram Implementation hints Instantiate philosopher array of five threads Instantiate array of five chopsticks

Dining Philosopher Illustrates: 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 Section: The program must limit the number of threads executing a part of an algorithm Idempotent Operations Operations that can be repeated many times without harm A pickUp method allows a philosopher to pick up a chopstick that they already own without harm.