Object Oriented Programming. Week 10 Part 1 Threads

Similar documents
Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CMSC 132: Object-Oriented Programming II. Threads in Java

Module - 4 Multi-Threaded Programming

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Unit - IV Multi-Threading

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

7. MULTITHREDED PROGRAMMING

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

CIS233J Java Programming II. Threads

Synchronized Methods of Old Versions of Java

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Threads Chate Patanothai

Unit III Rupali Sherekar 2017

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Concurrent Programming using Threads

Animation Part 2: MoveableShape interface & Multithreading

Overview of Java Threads (Part 2)

Multithreading in Java Part 2 Thread - States JAVA9S.com

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

EDAF65 Processes and Threads

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

EDA095 Processes and Threads

Multithread Computing

G52CON: Concepts of Concurrency

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Dining philosophers (cont)

Multi-threading in Java. Jeff HUANG

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

JAVA. Lab 12 & 13: Multithreading

Object Oriented Programming (II-Year CSE II-Sem-R09)

Multithreaded Programming


Programming Java. Multithreaded Programming

Amity School of Engineering

Chapter 19 Multithreading

Threads Questions Important Questions

CMSC 330: Organization of Programming Languages

Chapter 32 Multithreading and Parallel Programming

By: Abhishek Khare (SVIM - INDORE M.P)

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Software Practice 1 - Multithreading

Concurrent Computing CSCI 201 Principles of Software Development

What is a thread anyway?

Introduction to Java Threads

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Multithreading using Java. Dr. Ferdin Joe John Joseph

EDA095 Processes and Threads

Threads in Java (Deitel & Deitel)

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Java Threads. COMP 585 Noteset #2 1

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Programmers and Parallelism

Performance Throughput Utilization of system resources

Multithreading Pearson Education, Inc. All rights reserved.

04-Java Multithreading

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

CS 556 Distributed Systems

Concurrent Programming

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

Object-Oriented Programming Concepts-15CS45

Java s Implementation of Concurrency, and how to use it in our applications.

MULTI-THREADING

CERTIFICATION OBJECTIVES

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Advanced Concepts of Programming

Threads. 3 Two-Minute Drill. Certification Objectives. Q&A Self Test. Start New Threads. Write Code That Uses wait(), notify(), or notifyall()

CS360 Lecture 12 Multithreading

But for better understanding the threads, we are explaining it in the 5 states.

JAVA - MULTITHREADING

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Chapter 6: CPU Scheduling

Java Threads. Written by John Bell for CS 342, Spring 2018

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Time-Sharing Operating Systems. EDA095 Processes and Threads. Process Creation. The Content of a Process

Threads & Concurrency

Unit 5 - Exception Handling & Multithreaded

CS211 Lecture: Concurrency, Threads; UML Activity Diagrams last revised October 11, 2007 Objectives

COE518 Lecture Notes Week 7 (Oct 17, 2011)

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

Threads & Concurrency

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

CS 351 Design of Large Programs Threads and Concurrency

CS211 Lecture: Concurrency, Threads; UML Activity Diagrams last revised October 5, 2009 Objectives

Contents. 6-1 Copyright (c) N. Afshartous

Multithreaded Programming

Robotics and Autonomous Systems

UNIT V CONCURRENT PROGRAMMING

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Model Requirements and JAVA Programs MVP 2 1

Transcription:

Object Oriented Programming Week 10 Part 1 Threads

Lecture Concurrency, Multitasking, Process and Threads Thread Priority and State Java Multithreading Extending the Thread Class Defining a Class that Implements Runnable Adding Another Thread Thread Coordination Week 10 2

Concurrency, Multitasking, Processes and Threads Week 10 3

Concurrency Concurrency is doing multiple things at the same time. E.g. Printing while editing a document E.g. Browser loading images with accepting input Multitasking is running multiple tasks in a program, requiring Starting multiple programs Coordinating the programs Ending the programs Week 10 4

Processes and Threads Processes have a self-contained execution environment Separate memory and I/O More computation to start Less interference between processes Java usually runs one process Threads is concurrency that shares an execution environment Running in same memory with same I/O Less computation to start Treads can interfere with each other Every process has at least one thread Week 10 5

Multitasking Multitasking is running multiple processes requiring Starting multiple programs Coordinating the programs Ending the programs Preemptive multitasking Each process is given a time slot to use the CPU The process is preempted when The time slot is over The process needs I/O (i.e. needs another process to run to read or write data) Cooperative multitasking (now rare) Programs yield to other programs Week 10 6

Multithreading Each process runs multiple threads of control A thread of control is sequence of instructions that runs in a process ( i.e. a program) A process may run multiple threads of control by trading off between them. A thread of execution is a program executed independently of other parts of the program. Week 10 7

Process VS Thread S.No Process 1 No Sharing of Memory 2 Can not Corrupt Data structures 3 Context switching is Expensive Thread Sharing of Memory and other data structures Can Corrupt Data Structures Context Switching is Chaeper Week 10 8

Week 10 9

Thread Priority and State Week 10 10

Thread Priority Each thread has a priority Priority is set by the setpriority(int newpriority) method Thread priorities are integers between 1 and 10 1 is the minimum priority 10 is the maximum priority The scheduler chooses the highest priority runnable thread when choosing next thread Week 10 11

Thread State Thread may be in one of four states: new, runnable, blocked, and dead A Thread, thread, is new when it is created When thread.start() is called, it moves from new to runnable. When it the thread.run() method terminates, it moves from runnable to dead When a thread blocks, it moves from runnable to blocked. When the reason for the block goes away, it moves from blocked to runnable Week 10 12

Reasons from Blocking A thread moves from runnable to blocked if It sleeps It is waiting for I/O It is waiting to acquire a lock It is waiting for a condition Week 10 13

Thread State Diagram 1. New 2. Runnable 3. Blocked 4. Dead Week 10 14

Scheduling threads The scheduler starts a new thread when A thread has used up its time slot A thread has become blocked A thread with a higher priority has become runnable The scheduler chooses the highest priority thread from the runnable threads. Week 10 15

Terminating Threads Threads terminate when the run() method of that thread exits To end a Thread, t 1) Call t.interrupt, which sets a flag 2) The Thread t must respond to the interrupt and exit. Interrupting the thread and having it exit allows the thread to clean up. Week 10 16

Java Multithreading Week 10 17

Java Multithreading Each thread is associated with a Thread object Multitasking multithreading Virtual machine executes each thread for a short time slice Thread schedule activates and deactivates threads. Week 10 18

Thread Class In java.lang package Constructors Thread() Thread(String name) Thread(Runnable r) Thread(Runnable r, String name) Week 10 19

Thread methods getname(): returns thread name getpriority(): returns thread priority setpriority(): sets the thread's priority isalive(): return true if thread is alive; false o.w. run(): entry point for thread (like main() for threads) sleep(long ms): sleep for ms milliseconds start(): start a thread interrupt(): interrupt a thread isinterrupted: true if interrupted; false 0.w. Week 10 20

Static Thread Methods currentthread(): the thread that is currently running Week 10 21

Runnable Interface Requires definition of the run() method public interface Runnable { public void run(); } Week 10 22

Defining Threads Two ways Define a class that implements Runnable and pass it to the Thread constructor Define a class that is a subclass of Thread In either case you need to define the run() method. Week 10 23

Extending Thread Class Week 10 24

Example Extended Class Constructor: sets name and sleep time Run method: prints messages four messages Sleeps sleep time milliseconds Prints a message Week 10 25

Example Using Extended Class Returns program start time Prints thread name, time and note Time elapsed is current time start time Define main() Set start time Print that main() started Create a thread and start it Print that main finished Week 10 26

Example Output Main starts Starts thread Main is done Thread prints four times The main thread finishes before the thread it started prints its first line. The JVM runs the thread until it completes Week 10 27

Defining Class that Implements Runnable Week 10 28

Example Runnable Class Only difference is it implements Runnable Week 10 29

Example Using Runnable Class Only difference is it calls the Thread constructor and passing in Runnable class Week 10 30

Adding Another Thread Week 10 31

Example: Adding Second Thread Thread 1 prints once a second Thread 2 prints once a half second Week 10 32

Example: Second Thread Output Main starts two thread and finishes Thread 2 prints first time Thread 1 prints first time Thread 2 prints second and third time Thread 1 prints second time Thread 2 fourth and last time Thread 1 third and fourth, finishing Week 10 33

Coordinating Threads Week 10 34

Coordinating Threads Our threads do not wait for each other Suppose we want the main function to wait for one of the other functions The join() method causes the calling thread to wait until the thread on which the method is called terminates Final void join() throws InterruptedException Week 10 35

Join Example 1: t2.join() Wait until t2 finishes Week 10 36

Example 1: Output Main terminates when Thread 2 finishes Week 10 37

Example 2: Waiting for t1 to finish Wait until t1 finishes Week 10 38

Example 2: Output Main terminates when Thread 1 finishes Week 10 39