Multithreading Chapter 23. 2 Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Multithreading The objectives of this chapter are:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
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.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
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.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Concurrent Programming and Threads Threads Blocking a User Interface.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
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 human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
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.
SurfaceView.
Multi-Threading in Java
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3.
1 Mouse Events See Figure See Example in Figure 11.28,
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
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.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.
Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Threaded Programming in Python
Multithreading Lec 23.
Chapter 13: Multithreading
Multi Threading.
Multithreaded Programming in Java
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
Threads Chapter 4.
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading Chapter 23

2 Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we need a computer to multitask Concurrency normally available in ________________________ Java provides built-in _______________ –Multithreading improves the performance of some programs

3 Thread States: Life Cycle of a Thread _________ state –Thread was just created Ready state –Thread’s __________ method invoked –Thread can now execute Running state –Thread is _______ a processor and running _______________ state –Thread has completed or exited –Eventually disposed of by system View diagram, Figure 16.1

4 Thread Priorities and Thread Scheduling Java thread priority –Priority in range ___________ Timeslicing –Each thread assigned time on the processor (called a _________________) –Keeps highest priority threads running

5 Priorities and Scheduling Priority 9 Priority 8 Priority 7 Priority 10 Priority 6 Priority 5 Priority 4 Priority 3 Priority 2 Priority 1 AB D C EF G H I JK Ready threads Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

6 Creating and Executing Threads Figure 16.3 Demonstrates –Constructing ____________ objects –Using Thread methods start and __________ –Creates 3 equal priority threads –Each is put to sleep for random number of milliseconds –When __________, it displays name, etc.

7 Thread Synchronization Java uses monitors for thread synchronization The sychronized keyword –Every synchronized method of an object has a _________________ –_____________ inside a synchronized method at a time –All other threads ___________ until method finishes –Next highest priority thread runs when method finishes

8 Producers and Consumers Producer –Generating ____________ Consumer –___________ and processes the output

9 Synchronization Problem –Sometimes the producer gets too far ___________ of the consumer The objects produced fill up the holding area (________) The producer must wait for space to place objects –Sometimes the consumer gets ahead of the producer There are no objects to be processed (______________) The consumer must wait for the producer

10 Producer/Consumer Relationship without Synchronization Buffer –__________________ memory region Producer thread –Generates data to add to buffer –Calls _____________ if consumer has not read previous message in buffer –Writes to _______ buffer and calls notify for consumer ________________ thread –Reads data from buffer –Calls wait if buffer empty _______________ threads to avoid corrupted data

11 Producer/Consumer Relationship without Synchronization View source code which establishes –Buffer, Figure 16.4Figure 16.4 An interface which specifies ______________ methods –Producer, Figure 16.5Figure 16.5 __________________ of Thread Uses a shared Buffer object Method run is overridden from Thread class Uses Buffer.set() method –Consumer, Figure 16.6Figure 16.6 Also a subclass of Thread, also uses shared Buffer Uses the _________________ method

12 Producer/Consumer Relationship without Synchronization View Figure 16.7 which implements the Buffer interfaceFigure 16.7 –Implements the get and set methods This ___________________________ object is used in Figure 16.8 programFigure 16.8 –Buffer object declared, instantiated –Also Producer and Consumer objects –_________________ call method start()

13 Producer/Consumer Relationship without Synchronization Example randomly called producer and consumer You should note that in some cases the data was _____________________ !! –Consumer reads values _________ producer generates –Consumer misses a value –Consumer reads _____________ multiple times We need to deal with problem so data is not corrupted

14 Producer/Consumer Relationship with Synchronization Solution is to synchronize the producer and consumer objects Figure 16.9 implements a buffer and synchronizesFigure 16.9 –Consumer consumes only _______ produces a value –________________ produces a value only after consumer consumes previous value produced –Condition variable occupiedBufferCount determines __________________________ Program which uses this, Figure 16.10Figure 16.10

15 Circular Buffer Features –Multiple memory cells –Produce item if one or more _________ cells –____________ item if one or more filled cells Caveats –Producer and consumers must be relatively _________________________ Otherwise buffer fills up or stays empty –Synchronization still necessary –Seek to __________________ buffer size minimizes thread-wait time

16 Circular Buffer Code in figures noted below set up circular buffer, producer, consumer –Figure 16.11Figure Implements RunnableOutput class Overrides run method –Figure 16.12Figure Loads values in buffer Instantiates a RunnableOutput object –Figure 16.13Figure Instantiates RunnableOutput object

17 Circular Buffer Figure CircularBufferFigure –Instantiates an ___________, the shared area –Methods get() and set() implemented Figure –Program to demonstrate use of circular buffer –__________________ Producer and Consumer objects –Note that it is a windowed application Sends output to _____________________

18 Daemon Threads Run for benefit of other threads –Do not prevent program from ________________ –Garbage collector is a daemon thread Set daemon thread with method setDaemon –Must be done at ____________________ Do not assign ___________________ to daemon thread –Will be terminated without warning –May prevent those tasks from completing properly

19 Runnable Interface May be necessary to extend a class that already extends a class __________________ Java does not allow a class to extend more than one class at a time –__________________________ for multithreading support Program that uses a Runnable object to control a thread –Creates a __________________ object –Associates the Runnable object with that Thread class

20 Runnable Interface Illustration of using a Runnable interface –Figure 16.16Figure Note methods start, stop, run