Overview of Java Threads (Part 3)

Similar documents
The Java Executor (Part 1)

Overview of Java Threads (Part 2)

Java Barrier Synchronizers: CountDownLatch (Part 1)

Java Barrier Synchronizers: CyclicBarrier (Part 1)

The Java ExecutorService (Part 1)

Java Monitor Objects: Synchronization (Part 1)

Benefits of Concurrency in Java & Android: Program Structure

Java 8 Parallel ImageStreamGang Example (Part 3)

Infrastructure Middleware (Part 1): Hardware Abstraction Layer (HAL)

Java Semaphore (Part 1)

Overview of Java s Support for Polymorphism

Infrastructure Middleware (Part 3): Android Runtime Core & Native Libraries

The Java FutureTask. Douglas C. Schmidt

Java 8 Parallel Stream Internals (Part 2)

Overview of Activities

Overview of Advanced Java 8 CompletableFuture Features (Part 3)

Overview of Layered Architectures

Java ReentrantLock (Part 1)

Overview of Java 8 Parallel Streams (Part 1)

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Lecture 7: Process & Thread Introduction

External vs. Internal Iteration in Java 8

Overview of Advanced Java 8 CompletableFuture Features (Part 2)

A Case Study of Gang of Four (GoF) Patterns : Part 7

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

Android Services & Local IPC: The Command Processor Pattern (Part 1)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Overview of C++: Part 1

Overview of Patterns

Overview of Java 8 Functional Interfaces

The Assignment-2 Specification and Marking Criteria

CS 556 Distributed Systems

Overview of Frameworks: Part 3

Android Services & Local IPC: Overview of Programming Bound Services

Java Threads and intrinsic locks

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

CIS233J Java Programming II. Threads

Review what constitutes a thread Creating threads general Creating threads Java What happens if synchronization is not used? Assignment.

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

Creating Java Programs with Greenfoot

Overview of Patterns: Introduction

CS 351 Design of Large Programs Threads and Concurrency

Overview. Topics in This Section

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

Concurrency Utilities: JSR-166

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

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

Java 8 Programming for OO Experienced Developers

Object Oriented Programming. Week 10 Part 1 Threads

What is a Thread? Individual and separate unit of execution that is part of a process. multiple threads can work together to accomplish a common goal

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Sorting. Sorting. Selection sort

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L


Inheritance and Polymorphism

CST242 Concurrency Page 1

Objects, Subclassing, Subtyping, and Inheritance

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

CSCE 110: Programming I

Scheme Quick Reference

System Programming. Practical Session 4: Threads and Concurrency / Safety

Java SE 8 Programmer I and II Syballus( Paper codes : 1z0-808 & 1z0-809)

Control Statements. if for while

Design Patterns. Structural Patterns. Oliver Haase

Inheritance Abstraction Polymorphism

Requirements. PA4: Multi-thread File Downloader Page 1. Assignment

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

Multithreading using Java. Dr. Ferdin Joe John Joseph

Advanced Programming Concurrency

Self-test Java Programming

Techniques of Java Programming: Concurrent Programming in Java

Handling Multithreading Approach Using Java Nikita Goel, Vijaya Laxmi, Ankur Saxena Amity University Sector-125, Noida UP India

16 Multiple Inheritance and Extending ADTs

Animation Part 2: MoveableShape interface & Multithreading

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Theme 2 Program Design. MVC and MVP

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Java Threads. COMP 585 Noteset #2 1

Multithreading Pearson Education, Inc. All rights reserved.

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

Multithreaded Programming Part I: General Techniques

TeenCoder : Java Programming (ISBN )

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

Rules and syntax for inheritance. The boring stuff

A Case Study of Gang of Four (GoF) Patterns: Part 3

EDAF65 Processes and Threads

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

CO Java SE 8: Fundamentals

Studying software design patterns is an effective way to learn from the experience of others

EDA095 Processes and Threads

Scheme Quick Reference

Everything is an object. Almost, but all objects are of type Object!

Loop Invariant Examples

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

C16b: Exception Handling

Concurrency CSCI 201 Principles of Software Development

Java SE 8 Programming

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Transcription:

Overview of Java Threads (Part 3) Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Learning Objectives in this Part of the Lesson Understand how Java threads support concurrency Learn how our case study app works Know alternative ways of giving code to a thread Learn how to pass parameters to a Java thread Know how to run a Java thread Recognize common thread mechanisms Appreciate Java thread happensbefore orderings Understand the implementation of the GCD concurrent app 2

Learning Objectives in this Part of the Lesson Understand how Java threads support concurrency Learn how our case study app works Know alternative ways of giving code to a thread Learn how to pass parameters to a Java thread Know how to run a Java thread Recognize common thread mechanisms Appreciate Java thread happensbefore orderings Understand the implementation of the GCD concurrent app Know the pros & cons of Java thread programming models 3

Runtime Behavior of the GCD Concurrent App 4

Runtime Behavior of the GCD Concurrent App Concurrently compute the greatest common divisor (GCD) of two # s, which is the largest integer that divides two integers without a remainder See en.wikipedia.org/wiki/greatest_common_divisor 5

Implementation of the GCD Concurrent App 6

Implementation of the GCD Concurrent App This app showcases various Java Thread methods & alternative ways of giving code to a Java thread See github.com/douglascraigschmidt/posa/tree/master/ex/m3/gcd/concurrent 7

Implementation of the GCD Concurrent App This app showcases various Java Thread methods & alternative ways of giving code to a Java thread Super class that logs various activity lifecycle hook methods to aid debugging 8

Implementation of the GCD Concurrent App This app showcases various Java Thread methods & alternative ways of giving code to a Java thread Main entry point into the app that handles button presses from the user 9

Implementation of the GCD Concurrent App This app showcases various Java Thread methods & alternative ways of giving code to a Java thread Computes the GCD of two numbers by extending the Thread super class 10

Implementation of the GCD Concurrent App This app showcases various Java Thread methods & alternative ways of giving code to a Java thread Computes the GCD of two numbers by 11implementing the Runnable interface

Pros & Cons of Java Thread Programming Models 12

Now that we ve examined the source code for the GCD concurrent app we ll summarize the pros & cons of the various Java thread programming models 13

Pros with extending Thread public class GCDThread extends Thread { private MainActivity mactivity; public void setactivity (MainActivity activity) { mactivity = activity; private int computegcd (int number1, number2) { public void run() { 14

Pros with extending Thread It s straightforward to extend the Thread super class public class GCDThread extends Thread { private MainActivity mactivity; public void setactivity (MainActivity activity) { mactivity = activity; private int computegcd (int number1, number2) { public void run() { 15

Pros with extending Thread It s straightforward to extend the Thread super class It also consolidates all state & methods in one place public class GCDThread extends Thread { private MainActivity mactivity; public void setactivity (MainActivity activity) { mactivity = activity; private int computegcd (int number1, number2) { public void run() { 16

Pros with extending Thread It s straightforward to extend the Thread super class It also consolidates all state & methods in one place This is useful when the thread must be manipulated during runtime configuration changes public class GCDThread extends Thread { private MainActivity mactivity; public void setactivity (MainActivity activity) { mactivity = activity; private int computegcd (int number1, number2) { public void run() { 17

Pros with extending Thread It s straightforward to extend the Thread super class It also consolidates all state & methods in one place This is useful when the thread must be manipulated during runtime configuration changes e.g., interrupting/restarting a running thread & reading/ writing its state public class GCDThread extends Thread { private MainActivity mactivity; public void setactivity (MainActivity activity) { mactivity = activity; private int computegcd (int number1, number2) { public void run() { See the upcoming lessons on Managing the Java 18 Lifecycle & Managing Multi-threaded Activity State

Cons with extending Thread public class GCDThread extends Thread { private int computegcd (int number1, number2) { public void run() { 19

Cons with extending Thread A subclass must extend the Thread superclass public class GCDThread extends Thread { private int computegcd (int number1, number2) { public void run() { 20

Cons with extending Thread A subclass must extend the Thread superclass This is restrictive since Java only allows one superclass per subclass! public class GCDThread extends Thread { private int computegcd (int number1, number2) { public void run() { See docs.oracle.com/javase/tutorial/java/iandi/subclasses.html 21

Pros of implementing Runnable public class GCDRunnable implements Runnable, implements Serializable, extends Random { private int computegcd (int number1, number2) { public void run() { 22

Pros of implementing Runnable A subclass can implement multiple interfaces public class GCDRunnable implements Runnable, implements Serializable, extends Random { private int computegcd (int number1, number2) { public void run() { See docs.oracle.com/javase/tutorial/java/concepts/interface.html 23

Pros of implementing Runnable A subclass can implement multiple interfaces Which enables it to extend a different superclass public class GCDRunnable implements Runnable, implements Serializable, extends Random { private int computegcd (int number1, number2) { public void run() { See docs.oracle.com/javase/tutorial/java/concepts/interface.html 24

Pros of implementing Runnable A subclass can implement multiple interfaces Runnables are flexible since they can be reused in other contexts public class GCDRunnable implements Runnable, { GCDRunnable runnablecommand = new GCDRunnable(); ExecutorService executor = Executors.newFixedThreadPool (POOL_SIZE); executor.execute (runnablecommand); See upcoming lesson on the 25 Java Executor framework

Cons of implementing Runnable public class GCDRunnable implements Runnable, { GCDRunnable runnablecommand = new GCDRunnable(); Thread thr = new Thread(runnableCommand); thr.start(); 26

Cons of implementing Runnable Yields more moving parts public class GCDRunnable implements Runnable, { GCDRunnable runnablecommand = new GCDRunnable(); Thread thr = new Thread(runnableCommand); thr.start(); 27

Cons of implementing Runnable Yields more moving parts e.g., Runnable & Thread are separate entities & must be managed/accessed separately public class GCDRunnable implements Runnable, { GCDRunnable runnablecommand = new GCDRunnable(); Thread thr = new Thread(runnableCommand); thr.start(); This decoupling get complicated if a program needs to access the 28 state of a runnable, but only holds a reference to the thread object..

In practice, Java & Android software often implements Runnable rather than extending Thread run() start() Thread Runnable run() MyRunnable MyThread run() run() Thread Thread(Runnable) start() 29

In practice, Java & Android software often implements Runnable rather than extending Thread Lambda expressions are becoming popular with Java 8-based platforms See www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764 30

End of Overview of Java Threads (Part 3) 31