Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

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

Concurrent Programming Abstraction & Java Threads
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
 2005 Pearson Education, Inc. All rights reserved Multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading The objectives of this chapter are:
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
 2007 Pearson Education, Inc. All rights reserved. 1 Ch23 Multithreading: OBJECTIVES In this chapter you will learn:  What threads are and why they are.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
 2006 Pearson Education, Inc. All rights reserved Multithreading.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
4.7.1 Thread Signal Delivery Two types of signals –Synchronous: Occur as a direct result of program execution Should be delivered to currently executing.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Chapter 6: CPU Scheduling
Advanced Operating Systems CIS 720 Lecture 1. Instructor Dr. Gurdip Singh – 234 Nichols Hall –
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
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.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
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.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
T HREADS Lecture 5 1 L. Mohammad R.Alkafagee. H EAVYWEIGHT - PROCESSES The cooperation of traditional processes also known as heavyweight processes which.
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.
Embedded Computer - Definition When a microcomputer is part of a larger product, it is said to be an embedded computer. The embedded computer retrieves.
1.  System Characteristics  Features of Real-Time Systems  Implementing Real-Time Operating Systems  Real-Time CPU Scheduling  An Example: VxWorks5.x.
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.
Chapter 4 – Thread Concepts
Multithreading / Concurrency
Advanced Operating Systems CIS 720
Multithreading.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
23 Multithreading.
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.
TDC 311 Process Scheduling.
Multithreading.
Multithreaded Programming
Multithreading in java.
Threads and Multithreading
Presentation transcript:

Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.

 Operating systems on single-processor computers create the illusion of concurrent execution by rapidly switching between activities, but only a single instruction can execute at once.  Java makes concurrency available through the APIs.  You specify that an application contains separate threads of execution ◦ each thread has its own method-call stack and program counter ◦ can execute concurrently with other threads while sharing application-wide resources such as memory with them.  This capability is called multithreading. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Programming concurrent applications is difficult and error prone.  Guidelines: ◦ Use existing classes from the Java API that manage synchronization for you. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A thread can be in one of several thread states (new, runnable, waiting, time waiting, blocked, terminated).  A new thread begins in the new state.  Remains there until started, which places it in the runnable state.  A runnable thread can transition to the waiting state while it waits for another thread to perform a task. ◦ Transitions back to the runnable state only when another thread notifies it to continue executing. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A runnable thread can enter the timed waiting state for a specified interval of time. ◦ Transitions back to the runnable state when that time interval expires or when the event it’s waiting for occurs. ◦ Cannot use a processor, even if one is available.  A sleeping thread remains in the timed waiting state for a designated period of time (called a sleep interval), after which it returns to the runnable state.  A runnable thread transitions to the blocked state when it attempts to perform a task that cannot be completed immediately and it must temporarily wait until that task completes.  A runnable thread enters the terminated state when it successfully completes its task or otherwise terminates (perhaps due to an error). © Copyright by Pearson Education, Inc. All Rights Reserved.

 At the operating-system level, Java’s runnable state typically encompasses two separate states: ready or running.  When a thread first transitions to the runnable state from the new state, it is in the ready state.  A ready thread enters the running state (i.e., begins executing) when the operating system assigns it to a processor (known as dispatching the thread).  Typically, each thread is given a quantum or timeslice to perform its task.  This process is called thread scheduling. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Every Java thread has a thread priority that determines the order in which threads are scheduled.  Higher-priority threads should be allocated processor time before lower-priority threads.  Thread priorities cannot guarantee the order in which threads execute. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Most operating systems support time slicing, which enables threads of equal priority to share a processor.  An operating system’s thread scheduler determines which thread runs next.  One simple thread-scheduler implementation keeps the highest-priority thread running at all times and, if there’s more than one highest-priority thread, ensures that all such threads execute for a quantum each in round-robin fashion. This process continues until all threads run to completion. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When a higher-priority thread enters the ready state, the operating system generally preempts the currently running thread (an operation known as preemptive scheduling).  Higher-priority threads could postpone—possibly indefinitely—the execution of lower-priority threads. ◦ A situation known as starvation.  Operating systems generally employ a technique called aging to prevent starvation. ◦ Increasing the priority of a thread based on its waiting time. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Java provides higher-level concurrency utilities to hide much of this complexity and prevent errors in multithreaded programming.  Thread priorities are used behind the scenes too, but you will not be concerned with this as Java takes care of it. © Copyright by Pearson Education, Inc. All Rights Reserved.

 The Runnable interface declares the single method run, which contains the code that defines the task that a Runnable object should perform.  A Runnable object represents a “task” that can execute concurrently with other tasks.  When a thread executing a Runnable is created and started, the thread calls the Runnable object’s run method, which executes in the new thread.  To make your life even easier, Java provides the Executor interface to manage Threads. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Class PrintTask (Fig. 26.3) implements Runnable (line 5), so that multiple PrintTasks can execute concurrently.  Thread static method sleep places a thread in the timed waiting state for the specified amount of time. ◦ Can throw a checked exception of type InterruptedException if the sleeping thread’s interrupt method is called.  The code in main executes in the main thread, a thread created by the JVM.  The code in the run method of PrintTask executes in the threads created in main.  When method main terminates, the program itself continues running because there are still threads that are alive. ◦ The program will not terminate until its last thread completes execution. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When multiple threads share an object and it is modified by one or more of them, indeterminate results may occur unless access to the shared object is managed properly.  The problem can be solved by giving only one thread at a time exclusive access to code that manipulates the shared object. ◦ During that time, other threads desiring to manipulate the object are kept waiting. ◦ When the thread with exclusive access to the object finishes manipulating it, one of the waiting threads is allowed to proceed.  This process, called thread synchronization, coordinates access to shared data by multiple concurrent threads. ◦ Ensures that each thread accessing a shared object excludes all other threads from doing so simultaneously—this is called mutual exclusion. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A common way to perform synchronization is to use Java’s built-in monitors. ◦ Every object has a monitor and a monitor lock (or intrinsic lock). ◦ Can be held by a maximum of only one thread at any time. ◦ A thread must acquire the lock before proceeding with the operation. ◦ Other threads attempting to perform an operation that requires the same lock will be blocked.  To specify that a thread must hold a monitor lock to execute a block of code, the code should be placed in a synchronized statement. ◦ Said to be guarded by the monitor lock © Copyright by Pearson Education, Inc. All Rights Reserved.

 The synchronized statements are declared using the synchronized keyword:  synchronized ( object ) { statements } // end synchronized statement  where object is the object whose monitor lock will be acquired ◦ normally refers to this (i.e. the current object) if it’s the object in which the synchronized statement appears.  When a synchronized statement finishes executing, the object’s monitor lock is released.  Java also allows synchronized methods.  For performance reasons, synchronized methods and synchronized blocks should be as short as possible. © Copyright by Pearson Education, Inc. All Rights Reserved.

26.4 Synchronization Method

 Check the class OpenLab site for new Labs  Check Blackboard for new Quizzes  Continue working on the Project  Study for the Second Exam © by Pearson Education, Inc. All Rights Reserved.