21 Threads.

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.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
 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.
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-
Multithreading in Java Nelson Padua-Perez Bill Pugh 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.
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.
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.
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.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Threads.
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 in JAVA
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.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
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.
THREAD MODEL.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
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.
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.
Concurrent Programming in JAVA Steve Pruett - Introduction Jaime Mendez – Thread Creation Adrian Garcia – Thread Management Eric Orozco – Thread Safety.
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:
Chapter 4 – Thread Concepts
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Doing Several Things at Once
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Operating System (013022) Dr. H. Iwidat
Lecture 21 Concurrency Introduction
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Multithreading.
Multithreading.
Multithreaded Programming
Threads and Concurrency
Concurrency, Processes and Threads
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

21 Threads

Previously Properties of OO Programming Encapsulation Classes The Taxonomy of Insects Hierarchical Taxonomy Objects – Data Structures that Inherit Multiple Inheritance Polymorphism Dynamic Method Binding

Overview Concurrency Multiple processes vs Threads Why Threads? Class vs Interface Thread life cycle

Concurrency "a property of systems in which several computations are executing simultaneously, and potentially interacting with each other" E.g. a 2-player computer fighting game - both characters movements and actions are independent of the other

Why Threads? To perform a time-consuming task without locking up the event-dispatching thread. making extensive calculations, doing something that results in many classes being loaded (initialization, for example) blocking for network or disk I/O. To perform an operation repeatedly, usually with some predetermined period of time between operations. To wait for messages from clients.

Multiple processes vs Threads Threads are lightweight compared to processes Threads share the same address space and therefore can share both data and code Context switching between threads is usually less expensive than between processes Cost of thread intercommunication is relatively low that of process intercommunication Threads allow different tasks to be performed concurrently.

Class or Interface? Two ways to create a thread in Java: Extend the Thread class Implement the Thread interface Remember there is no multiple inheritance in Java. So ONLY extend the Thread class if no other classes are being extended Otherwise implement the Thread Interface Threads are part of java.lang so no need to import anything- they're always there!!!

Thread Class A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running

Thread Interface A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception

Waiting/Blocking/Sleeping Thread life cycle JVM setPriority(int iPriorty) iPrioririty with values 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY) Exiting the run method moves the thread to the “Dead” state new Thread() Threadi New notify() notifyAll() start() Runnable Waiting/Blocking/Sleeping Scheduler yield() Scheduler Running Alive join() wait() sleep() Syncronized ... Dead Dead threads cannot be brought back to life call to start() on a dead thread will get a runtime exception