Multithreading.

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

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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Definitions Process – An executing program
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-
Java Threads CS Introduction to Operating Systems.
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.
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. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
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)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
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.
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.
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.
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.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
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.
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.
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.
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.
THREAD MODEL.
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.
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.
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Java Thread Programming
Tutorial 2: Homework 1 and Project 1
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Multithreading.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Multithreading in Java
More About Threads.
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 Chapter 23.
Multithreading 2 Lec 24.
Java Based Techhnology
Multithreading.
Multithreaded Programming
21 Threads.
Java Thread.
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

Objectives Need for Multithreading Life Cycle of a Thread Understand Multitasking Need for Multithreading Implementing Multithreading in Java Life Cycle of a Thread Understanding Thread Priorities Synchronization Inter-thread Communication

Multitasking Multitasking is a process that involves multiple tasks running simultaneously. OS implements Multitasking using Multiprocessing. Java implements Multitasking using Multithreading.

Multiprocessing Program is a set of instructions and a Process is a ‘Running Instance of a Program’. CPU is shared between 2 processes.

Multiprocessing In Multiprocessing, OS implements Context Switching mechanism. Program’s entire context including variable, global variables, methods etc. is stored separately.

Multithreading Different tasks within a main task execute simultaneously. Multithreading implements the idea of Multitasking by taking it one level lower.

Multithreading Individual programs appear to do multiple tasks simultaneously. Each sub task within an application is called as a Thread.

Thread & Its Need A Thread is an entity within a Process. It defines the path of execution. Need: To run multiple tasks simultaneouly. To make effective use of CPU. In Java, by default, ‘main()’ is one thread and ‘Garbage Collector’ is another thread.

Process and Thread Each process has a complete set of its own variables. It takes more overhead to launch a new process. Inter-process communication is a heavyweight activity. Threads live in a single application, thus may share the same data. It takes much less overhead to create and destroy a thread. Inter-thread communication is a lightweight activity.

Implementing Multithreading in Java Multithreading in Java can be implemented by using one of the following approaches: Extending Thread class. Implementing Runnable interface.

Life Cycle of Thread start sleep interval expires IO Complete resume BORN start sleep interval expires IO Complete READY resume notify/notifyAll suspend RUNNING wait sleep IO Request WAITING SLEEPING BLOCKED SUSPENDED stop complete DEAD

Thread Priorities Thread Priorities are used by Scheduler. Threads of equal priority complete equally for CPU. The scheduler picks up the highest priority thread.

Thread Priorities Priority levels can be set within the range MIN_PRIORITY(1) and MAX_PRIORITY(10). Default priority is NORM_PRIORITY(5).

Methods of Thread Class start() stop()//Deprecated yield() isAlive() sleep() suspend()//Deprecated resume()//Deprecated currentThread() join()

start() A method that makes a request to OS for the creation of the Thread. Responsible for transitioning of the thread from BORN state to READY state.

stop() - Deprecated Forcefully kills the thread. Sends the thread into DEAD state. Once a thread becomes DEAD, cannot be started again.

yield() A static method that causes currently executing thread to yield the control. If there are other runnable threads whose priority is at least as high as this thread, they will be scheduled next. Again, everything is dependent upon OS.

isAlive() Returns true if a thread has started and not yet terminated. A thread is dead if run() method executes normally. It dies abruptly because of an uncaught exception.

sleep() A static method that sends a thread into the ‘sleeping’ state. A thread remains in the ‘sleeping’ state until the sleep time interval is over. Time interval is specified in terms of milliseconds.

suspend() and resume() - Deprecated Sends a thread into the ‘suspended’ state. Deprecated as the transitioning into the READY state is implementation dependent. resume(): Brings the thread into the ‘ready’ state from the suspended state.

currentThread() A method that returns a reference of the currently running thread. E.g. public void run(){ if(Thread.currentThread() == t1){ //Some Code }

join() Waits for a thread to terminate. E.g. public void myMethod()throws Exception{ Thread t = ……..; t.join(); //These statements execute only after the death of Thread t. }

Synchronization

Synchronization Many times, 2 or multiple threads need to share access to the same object. This needs to ensure that the object will be modified by only one thread at a time, else they will fall into ‘race condition’.

Synchronization Key to synchronization is the concept of monitor. A monitor is an object that is used as a mutually exclusive lock. Only one thread can own a monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will get suspended, until the first thread exits the monitor.

Inter-Thread Communication

Inter-Thread Communication Sometimes, in an application there is a need of 2 or multiple threads interacting with each other. It leads to inter-thread communication. E.g. a communication between 2 threads like: Producer and Consumer or Feeder and Copier.

Inter-Thread Communication To implement inter-thread communication, there are 3 methods used: wait() notify() notifyAll()

wait() Must be invoked within a synchronized context. When invoked, releases the lock and sends the currently running thread into the ‘waiting’ state. Gives a chance to other threads looking for the same lock.

notify() & notifyAll() Wakes up a single thread that is in ‘waiting’ state. If multiple threads are waiting, then the thread that enters first into the ‘waiting’ state gets notified. notifyAll() Wakes up all threads.

DEADLOCKS When a thread enters the ‘waiting’ state, cannot be unblocked itself. The ‘waiting’ thread can be brought into READY state when other thread notifies by using either notify() or notifyAll(). If not handled properly, all the threads may go into the ‘waiting’ state and there is no one to notify them. This leads to DEADLOCK.