Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
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:
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.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Definitions Process – An executing program
1 Chapter 4 Threads Threads: Resource ownership and execution.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
Process Concept An operating system executes a variety of programs
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 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.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
SYNCHRONIZATION Module-4. scheduling Scheduling is an operating system mechanism that arbitrate CPU resources between running tasks. Different scheduling.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
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.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
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.
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.
Review for Quiz-2 Applied Operating System Concepts Chap.s 1,2,6,7 - ECE3055b, Spring 2005.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
1 OS Review Processes and Threads Chi Zhang
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
T HREADS Lecture 5 1 L. Mohammad R.Alkafagee. H EAVYWEIGHT - PROCESSES The cooperation of traditional processes also known as heavyweight processes which.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety 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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Background on the need for Synchronization
Applied Operating System Concepts -
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
23 Multithreading.
143a discussion session week 3
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.
Threads Chapter 4.
Multithreading The objectives of this chapter are:
Chapter 3: Process Management
Presentation transcript:

Threads in Java

History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties  OS schedules process A  Process A runs until it is interrupted  OS saves process A’s state  OS schedules process B  Process A has what is necessary to execute  OS interrupts process B, saving its state  OS schedules process A  Most programming languages do not have techniques to permit the user to specify concurrent activities  User wanting to program concurrently used operating system calls  ADA, developed for defense applications, was the first language to permit user-level concurrency  Java has tools that permit concurrency  threads, so-called lightweight processes (because they lack their own address space)  These are portable across platforms

Uses  Single-threaded applications can lead to long delays  E.g., a process waiting for i/o must get what it needs until it can resume  While that process is waiting, no other process can run  Example  User is downloading an audio clip  If single threaded, user must wait for the clip to be downloaded before he/she can listen.  If multi-threaded, one thread does download another plays back. The threads are synchronized so that listening does not begin until there is something to listen to.  Multithreading is difficult and error-prone.

Thread States  New: thread has been created  Runnable: executing its task  Waiting: waiting for another thread to perform a task  Timed Waiting: sleeping for a specific amount of time  Blocked: waiting for resources  Terminated: thread is complete

Examples  Timed Waiting  Word processor that periodically saves work  If thread did not sleep, it would have to be in a continuous loop, asking the main process if it’s time to back up  consumes resources  Blocked  Thread issues i/o request.  Thread cannot resume until the request has been fulfilled.  Runnable  Collapses two o/s states: ready and running

Scheduling  The JVM schedules threads by priority  The scheduling algorithm used depends on the os  All multiuser os support some form of time-slicing  Many possibilities  Round robin  Multi-level priority queue (p. 1049)

Implementing Threads Directly  Implement the Runnable interface  Method run() contains the code that the thread will execute  Examples: ThreadCreator/PrintTask

Using the Executor/Service Interfaces  Executor  Manages the execution of Runnable objects  Collects a group of threads into a thread pool  Reuses existing threads to eliminate the overhead involved in creating threads  Optimizes the number of threads to ensure that processor stays busy  ExecutorService  Interface that extends Executor and adds additional methods to manage thread lifecycle  Examples: TaskExecutor/PrintTask

Synchronization  Issue  Multiple threads of control  Single Address space  Imagine that we have two control structures  Parbegin  initializes parallel processing  Parend  ends parallel processing  Look at the following scenario

Withdraw(){ Record acctRec; parbegin{paulProc();heidiProc();}} ATM Example

Parallel Processes paulProc(){ read request; if (acctRec.bal >= request) { issue request; acctRec.bal -= request; acctRec.bal -= request; }}paulProc(){ read request; if (acctRec.bal >= request) { issue request; acctRec.bal -= request; acctRec.bal -= request; }}

Critical Section  The part of the program where shared memory is accessed

Race Condition  Two or more processes/threads reading/writing shared data  Result depends on who gets there first

Mutual Exclusion  Mechanism to prevent more than one process from entering its critical section  Need for such a mechanism was recognized in the sixties  Most languages do not provide program- level mutual exclusion facilities  Programmer must rely on operating system calls

Monitor  Java’s solution to the mutual exclusion problem  Every object has a monitor and a monitor lock  Monitor ensures that its object’s lock is held by a maximum of one thread at a time.  If a thread holds a lock, other threads trying to access the same object will be blocked until the thread is released.

High Level  Synchronized statement  Specifies that a thread must hold a monitor lock to execute the synchronized statements.  Monitor allows only one thread at a time to execute statements within a synchronized block  When a thread finishes executing the synchronized statements, the lock is released. Synchronized ( object ) {statements}

Producer/Consumer Problem  Class IPC problem  There is a buffer shared by  A producer who produces objects  A producer who consumes objects  Simplest approach: strict alternation  Consumer must not consume before producer produces  Producer must not produce object B before object A has been consumed