MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Similar documents
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

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

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

CS420: Operating Systems

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Threads Questions Important Questions

Object Orientated Programming in Java. Benjamin Kenwright

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

OPERATING SYSTEM. Chapter 4: Threads

7. MULTITHREDED PROGRAMMING

Administrivia. Events this week Drop-In Resume and Cover Letter Editing Date: Tues., Mar 23 Time: 12:30 2:30 pm Location: Rm 255, ICICS/CS

CMSC 132: Object-Oriented Programming II

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

CS 556 Distributed Systems

POSIX Threads: a first step toward parallel programming. George Bosilca

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

Synchronization in Concurrent Programming. Amit Gupta

Chapter 4: Multi-Threaded Programming

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

Chapter 4: Threads. Chapter 4: Threads

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

Chapter 4: Threads. Chapter 4: Threads

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency: State Models & Design Patterns

Multi-threaded programming in Java

CIS233J Java Programming II. Threads

Introducing Shared-Memory Concurrency

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Threading and Synchronization. Fahd Albinali

CS 450 Operating System Week 4 Lecture Notes

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

Multithreading using Java. Dr. Ferdin Joe John Joseph

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

Threads Chapter 5 1 Chapter 5

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

Threads Chate Patanothai

CS307: Operating Systems

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Chapter 2: Operating-System Structures. Operating System Concepts 8 th Edition

Chapter 4: Multithreaded

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Threads Assistant Professor DCS Operating System Concepts

CSE 374 Programming Concepts & Tools

Performance Throughput Utilization of system resources

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

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

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

Concurrent Programming using Threads

CS360 Lecture 10 Multithreading

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

Advanced Flow of Control -- Introduction

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CHAPTER 6: PROCESS SYNCHRONIZATION

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

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

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Inheritance and Interfaces

Reintroduction to Concurrency

Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Threads, Concurrency, and Parallelism

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Multithreaded Programming

Lecture 7: Process & Thread Introduction

Object Oriented Programming. Week 10 Part 1 Threads

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

CS370 Operating Systems Midterm Review

Java Threads. COMP 585 Noteset #2 1

Chapter 4: Threads. Operating System Concepts 9 th Edition

Process Description and Control

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 4: Multithreaded Programming

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

THREADS & CONCURRENCY

Multithread Computing


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

Chapter 4: Threads. Operating System Concepts 9 th Edition

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

4. Concurrency via Threads

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

CISC2200 Threads Spring 2015

Lecture 4: Process Management

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 19 Multithreading

Robotics and Autonomous Systems

Chapter 4: Multithreaded Programming

Transcription:

MultiThreading Object Orientated Programming in Java Benjamin Kenwright

Outline Review Essential Java Multithreading Examples Today s Practical Review/Discussion

Question Does the following code compile? What would the output be?

Answer

Question Does the following code compile? What would the output be?

Answer 78-7616 Explain why?

Question Does the following code compile? What would the output be?

Answer

Question Does the following code compile? What would the output be?

Answer

Question Does the following code compile? What would the output be?

Answer Yes abcd abc false abcd abcd true

Why Multithreading? What is the rational? Why make things complicated? What would happen if we didn t have multithreading?

Concurrency & Parallelism

Threading Advantages & Disadvantages of Threads Java Threads Class: java.lang.thread Interface: java.lang.runnable Multithreaded Programming

Thread Definition Definition: A thread is a single sequential flow of control within a program (also called lightweight process)

Thread Each thread acts like its own sequential program Underlying mechanism divides up CPU between multiple threads Two types of multithreaded applications Make many threads that do many tasks in parallel, i.e., no communication between the threads (GUI) Make many threads that do many tasks concurrently, i.e., communication between the threads (data access)

Advantages/Disadvantages Advantages Responsiveness, e.g., of user interfaces Resource sharing Economy Utilization of multiprocessor hardware architectures Disadvantages More complicated code Deadlocks (very hard to debug logical program errors)

Single & Multithreaded Processes

User and Kernel Threads Thread management done by user-level threads library Supported by the kernel

Java Threads Java threads may be created by Extending Thread class Implementing the Runnable interface

Class Thread The simplest way to make a thread Treats a thread as an object Override the run() method, i.e., the thread s main Typically a loop Continues for the life of the thread Create Thread object, call method start() Performs initialization, call method run() Thread terminates when run() exits

Extending the Thread Class

Extending the Thread Class Example javac ThreadTest.java java -cp. ThreadTest

Multithreaded Programming

Sharing Resources Single threaded programming: you own everything, no problem with sharing Multi-threaded programming: more than one thread may try to use a shared resource at the same time Add and withdraw from a bank account Using the speakers at the same time, etc. Java provides locks, i.e., monitors, for objects, so you can wrap an object around a resource First thread that acquires the lock gains control of the object, and the other threads cannot call synchronized methods for that object

Locks One lock pr. object for the object s methods One lock pr. class for the class static methods Typically data is private, only accessed through methods Must be private to be protected against concurrent access If a method is synchronized, entering that method acquires the lock No other thread can call any synchronized method for that object until the lock is released

Sharing Resources, cont. Only one synchronized method can be called at any time for a particular object

Sharing Resources, cont. Efficiency Memory: Each object has a lock implemented in Object Speed and Overhead (e.g., calling) Older standard Java libraries used synchronized a lot, did not provide any alternatives

Sharing Resources, Example

Sharing Resources, Example

Sharing Resources, Example cont.

Sharing Resources, Example cont.

Sharing Resources, Example cont.

The Runnable Interface To inherit from an existing object and make it a thread, implement the Runnable interface A more classical, function-oriented way to use threads

The Runnable Interface, cont.

The Runnable Interface, cont.

Java Thread Management suspend() suspends execution of the currently running thread sleep() puts the currently running thread to sleep for a specified amount of time resume() resumes execution of a suspended thread stop() stops execution of a thread

Synchronized Fields and Constructors

Issues Thread priority Thread groups Daemon (unix term) similar to a service (on Win32) Deadlock Very hard to detect logical errors in programs

Deadlocks

Deadlocks, cont.

Summary Overview Multithreading with Java Single-threaded programming: live by all by your self, own everything, no contention for resources Multithreading programming: suddenly others can have collisions and destroy information, get locked up over the use of resources Multithreading is built-into the Java programming language Multithreading makes Java programs complicated Multithreading is by nature difficult, e.g., deadlocks

This Week Read Associated Chapters Review Slides Java Exercises

Exercises Chapter 29 Exercises 29.1 to 29.4

Questions/Discussion