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.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Java Overview Part II Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Multithreading The objectives of this chapter are:
Software Engineering Oct-01 #11: All About Threads Phil Gross.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Java Programming: Advanced Topics
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.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Java Threads Representation and Management of Data on the Internet.
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.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Concurrent Programming and Threads Threads Blocking a User Interface.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
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
Java Thread and Memory Model
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.
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 in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multi-Threading in Java
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.
1 OS Review Processes and Threads Chi Zhang
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.
1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Multithreading.
Multithreading.
Multithreaded Programming
21 Threads.
Computer Science 2 06A-Java Multithreading
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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 Java Network Programming And Distributed Computing

Spring 2006Special Topics in Computer Engineering : Threads 2 Topics Threads Synchronization Groups/Priorities Inter-thread communication

Spring 2006Special Topics in Computer Engineering : Threads 3 Concurrent Programming What is a Thread? What can go wrong with a Thread? How can we fix it? How does Java handle them? Why would I use them?

Spring 2006Special Topics in Computer Engineering : Threads 4 What is a Thread? A Thread is a single, sequential flow of control within a program. Now they are so ubiquitous (yū-bĭk'wĭ-təs) that you don’t notice any them more.  DOS vs. Windows DOS vs. Windows

Spring 2006Special Topics in Computer Engineering : Threads 5 ubiquitousubiquitous (yū-bĭk'wĭ-təs) everywhere ever-present omnipresent

Spring 2006Special Topics in Computer Engineering : Threads 6 Processes vs. Threads Processes  Completely separate, unrelated concurrent execution on the level of the operating system. (eg multiple programs running at the same time) Threads  Concurrent units of execution within a given program. (eg pulling down a menu while loading a web page within a web browser)

Spring 2006Special Topics in Computer Engineering : Threads 7 Thread States New state  Thread was just created Ready state  Thread can now execute In Java, Thread’s start method invoked Running state  Thread is assigned a processor and running In Java, Thread’s run method is executing Done state  Thread has completed or exited  Eventually disposed of by system

Spring 2006Special Topics in Computer Engineering : Threads 8 Life cycle of a Thread NewReady Blocked Running Done Thread object is created start method was invoked run method is executing

Spring 2006Special Topics in Computer Engineering : Threads 9 Life cycle of a Thread (cont’d) The OS can interrupt the thread at any time while it is running, and allow any other thread to run. Threads can put themselves into a wait state until another thread wakes them up.

Spring 2006Special Topics in Computer Engineering : Threads 10 How does Java handle Threads? Subclass java.lang.Thread, or implement java.lang.Runnable. New After you instantiate a thread, it is in the New state. To start running a Thread, call the start method on it. Ready Running  thread transits to Ready state and when OS schedule it for execution it transits to the Running state

Spring 2006Special Topics in Computer Engineering : Threads 11 How does Java handle them? (cont’d) To implement Runnable, you need to have a method that override public void run(); This is the method that implements the running section of the thread life-cycle. The thread dies (stops) when the run method terminates.

Spring 2006Special Topics in Computer Engineering : Threads 12 How does Java handle them? (cont’d) Blocked The run method may be interrupted at any time by the operating system and put into the Blocked state, but that’s not something you really need to handle.

Spring 2006Special Topics in Computer Engineering : Threads 13 How does Java handle them? (cont’d) Run methods generally look like: public class SomeThread extends Thread { public void run() { while (notDone) {…} } public void finish() { notDone = false; } }

Spring 2006Special Topics in Computer Engineering : Threads 14 java.lang.Thread Class Provides methods to start suspend resume stop control other aspects (e.g.)  priority  name

Spring 2006Special Topics in Computer Engineering : Threads 15 Using the Thread Class Extend Thread class Create a Thread object Start thread running  Invoke Thread.start() method  run() method will be invoked when thread is first started Override the run() method  thread performs useful tasks in the background

Spring 2006Special Topics in Computer Engineering : Threads 16 Example ExtendThreadDemo

Spring 2006Special Topics in Computer Engineering : Threads 17 What can go wrong? Assuming that threads, existing in the same program, have access to the same variable (e.g. countValue ). What if one is reading data from countValue, gets interrupted, and another one writes to countValue, even though the thread wanted the old value?

Spring 2006Special Topics in Computer Engineering : Threads 18 What can you do about it? synchronized is a keyword that can be applied to a method to say that one and only one thread will have access to this method at a time. public synchronized void blah(){ }

Spring 2006Special Topics in Computer Engineering : Threads 19 Method–Level Synchronization t1 invokes synchronized method  t1 takes out an object lock (monitor)  t2 attempts to execute synchronized method t2 is BLOCKED  t3,… tn attempt to execute synchronized method, all are BLOCKED and queued t1 returns from synchronized method  lock on the object monitor released  one of the queued threads, ti, access object lock ti starts executing synchronized method

Spring 2006Special Topics in Computer Engineering : Threads 20 Example: Counter Update Problem Value of counter  incremented  read At the same time several threads invoke a method to update counter  overwrite each others update A read gets an inaccurate count value

Spring 2006Special Topics in Computer Engineering : Threads 21 Counter Update: Solution synchronizing each method that performs a read or write Thread that first invokes the synchronized method locks the counter object’s monitor Lock is released only when thread returns from synchronized method Result: only one thread can update/read counter at a time

Spring 2006Special Topics in Computer Engineering : Threads 22 Counter Update: Sample Synchronized Code Sample Synchronized Code

Spring 2006Special Topics in Computer Engineering : Threads 23 Block-Level Synchronization Used for preventing concurrent access to resource that is a preexisting class  developer cannot modify by using method- level synchronization A lock is placed around a block of code  block of code is synchronized against a particular object  any thread attempting to enter that block of code is locked out until the monitor for the specified object is released

Spring 2006Special Topics in Computer Engineering : Threads 24 Block-Level Synchronization: Syntax { …synchronized (o) { … } }

Spring 2006Special Topics in Computer Engineering : Threads 25 More on synchronized public synchronized void blah() {…} Is the same as public void blah (){ synchronized (this){…} }

Spring 2006Special Topics in Computer Engineering : Threads 26 java.lang.Runnable interface A better way create a multi-threaded application  Java supports only single inheritance a class that extends the java.lang.Thread class, cannot extend any other class Runnable defines a single method, run(), that must be implemented

Spring 2006Special Topics in Computer Engineering : Threads 27 Using Runnable Pass an object implementing Runnable to the constructor of a thread Invoke thread’s start() method  run() method of Runnable will be called by newly created thread  Thread stops executing when run() method terminates

Spring 2006Special Topics in Computer Engineering : Threads 28 Using Runnable for Multi-Threading Example: RunnableThreadDemoRunnableThreadDemo

Spring 2006Special Topics in Computer Engineering : Threads 29 Illustrating Block-Level Synchronization Block-level synchronization locks against a particular object  multiple blocks can protect access to the same object Block-level synchronization can be applied in thread code wherever an object is accessed or modified

Spring 2006Special Topics in Computer Engineering : Threads 30 Block-Level Synchronization Example SynchBlock implements Runnable

Spring 2006Special Topics in Computer Engineering : Threads 31 More that can go wrong What happens if you have two things that do this - deadlock public void doSomething() { Synchronized (a) { Synchronized (b) { // code } } } public void doOther() { Synchronized (b) { Synchronized (a) { // code } } }

Spring 2006Special Topics in Computer Engineering : Threads 32 Thread examples Test: Ten threads printing their name three times. Test Test2: Main thread joins printing threads. Test2 Test3: Each thread yields after printing. Test3 Test4: Printing threads yield randomly. Test4

Spring 2006Special Topics in Computer Engineering : Threads 33 Advanced Thread Features All Java Threads have a priority. If you want a thread to run more relative to other threads, give it a higher priority. Threads can be grouped in ThreadGroup objectsThreadGroup Test7: Two groups of threads Test7 Test8: Printing threads with priority Test8

Spring 2006Special Topics in Computer Engineering : Threads 34 Why would I use them? Most advance programs rely on Threads for various tasks. ThreadLister Example 2 cases:  When you want to be doing 2 different things simultaneously.  When you have a large problem that can be broken up and solved in smaller sections, or large I/O bound processes.

Spring 2006Special Topics in Computer Engineering : Threads 35 Inter-Thread Communication Sometimes one thread may be interested in the activities of another. Or, one could have a functional dependency on another.  Reading from a file or over a network?  Waiting for a given thread to return a result. Polling (Busy Waiting) vs. Notification  BadConsumer Example BadConsumer

Spring 2006Special Topics in Computer Engineering : Threads 36 Waiting for notification As defined in object, every object has a wait(), notify(), and notifyAll() method.  These should never be overridden They can only be called from inside synchronized blocks, and they only effect other threads in synchronized blocks which are synchronized on the same object.

Spring 2006Special Topics in Computer Engineering : Threads 37 wait() (cont’d) When a thread enters a wait state, it does nothing until it is notified by another thread. It also gives up it’s lock on the object when wait is called. public synchronized blah() { wait(); … // do something }

Spring 2006Special Topics in Computer Engineering : Threads 38 notify() To awaken a thread, a different thread which has a lock on the same object must call notify. When notify is called, the block that had the lock on the object releases it.  Then a thread is awakened from its wait() and can grab the lock and continue processing.

Spring 2006Special Topics in Computer Engineering : Threads 39 notify() (cont’d) Note that you don’t specify what is being awoken in notify(). If there are more than 1 thread waiting on the same condition, you have no control of which awakens. notify() only awakens 1 thread. notifyAll() awakens all threads.

Spring 2006Special Topics in Computer Engineering : Threads 40 notifyAll() (cont’d) There are two versions - notify() and notifyAll(). Notify is safe only under 2 conditions:  When only 1 thread is waiting, and thus guaranteed to be awakened.  When multiple threads are waiting on the same condition, and it doesn’t matter which one awakens. In general, use notifyAll()