15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

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)
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:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Definitions Process – An executing program
1 MATERI PENDUKUNG SINKRONISASI Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
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.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
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.
11 Other Programming Aspects Dr. Miguel A. Labrador Department of Computer Science & Engineering
Java Programming: Advanced Topics
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
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.
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.
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.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Introduction to Threads Computers can perform many tasks concurrently – download a file, print a file, receive , etc. Sequential languages such.
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
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
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.
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.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
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.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
Distributed and Parallel Processing George Wells.
Java Thread Programming
Multithreading The objectives of this chapter are:
Multithreading / Concurrency
Multi Threading.
Multithreading.
Multithreaded Programming in Java
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.
Multithreading.
Computer Science 2 06A-Java Multithreading
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Synchronization and liveness
Presentation transcript:

15.1 Threads and Multi- threading

Understanding threads and multi-threading In general, modern computers perform one task at a time It is often desirable to perform several tasks simultaneously Some operating systems can load several programs at once, then give them shared access to the CPU This is multi-threading, and gives the appearance that the programs are running simultaneously

Thread class An instance of a program that runs simultaneously with other programs is a thread The java.lang.Thread class models a thread of execution To create a new thread of execution, extend Thread, then put your code in Thread’s run() method To use it, instantiate the class and call it’s start() method (NOT run())

Runnable interface If you do not wish to extend Thread, create a class that implements the Runnable interface Define your run() method in this class Create a new Thread, passing your Runnable class as an argument, then call the Thread’s start() method

15.3 Creating and Running Threads Create a class that extends Thread –Override the run() method with your code –Instantiate your new class –Call it’s start() method Create a class that implements Runnable –Create a run() method that holds your code –Create an instance of Thread, passing your class to the constructor –Call the Thread’s start() method

Running threads The only way to start a Thread is by calling the Thread’s start() method –Not its run() method –Not a start() method of the class implementing the Runnable interface

Thread states Born –The Thread object has been created Ready –The Thread’s start() method has been called, but it has not yet been allocated CPU time Running –The Thread’s run() method is being executed by the JVM Blocked –The Thread is waiting for an event to occur Dead –The Thread’s run method has completed

Blocked state Blocked Threads allow other Threads to access the CPU Thread.sleep() places the calling Thread into a blocked state for an amount of time Thread.join() cause a Thread to wait until the completion of the called Thread Thread.yield() allows other Threads of the same priority to access the CPU

Thread scheduling Obtain a reference to the current Thread using the Thread.currentThread() static method Use the isAlive() method to determine if a Thread is dead or not Use the setPriority() method to control the Thread’s priority

Stopping threads Older methods that stopped Threads are unreliable Use a flag in the run() method to indicate when the Thread should end

Synchronization and deadlocks Sometimes, different Threads can attempt to access the same data Two Threads may be executing the same method at the same time The contents of local variables may becoming unpredictable

Synchronization and deadlocks Blocks of statements can be declared synchronized Only one Thread at a time can access a synchronized block When two Threads try to access the same synchronized block, one must wait Every Object has a flag or lock A Thread must obtain the object’s lock before it can access the synchronized code

Synchronization and deadlocks A Thread waits until the object’s lock becomes available It is possible that two Threads could prevent the release of the lock the other is waiting for Neither Thread can progress; this is called deadlock Java does not have any mechanisms to detect or prevent deadlock

Communication between threads Threads can be used to query the status of an object Polling is an inefficient means of querying An alternative is to force the querying Thread to wait When the object’s status changes, another Thread calls notify for that object All waiting Threads then become eligible to continue