G52CON: Concepts of Concurrency

Similar documents
Object Oriented Programming. Week 10 Part 1 Threads

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency

Threads Chate Patanothai

7. MULTITHREDED PROGRAMMING

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Multithreading in Java Part 2 Thread - States JAVA9S.com

Threads in Java (Deitel & Deitel)

Multithreaded Programming

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

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

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Model Requirements and JAVA Programs MVP 2 1

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

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

CS 556 Distributed Systems

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

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

Unit - IV Multi-Threading

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

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

Java Threads. COMP 585 Noteset #2 1

G52CON: Concepts of Concurrency

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

Concurrent Computing CSCI 201 Principles of Software Development

Animation Part 2: MoveableShape interface & Multithreading

Chapter 19 Multithreading

G52CON: Concepts of Concurrency

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

CS 351 Design of Large Programs Threads and Concurrency

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

Advanced Concepts of Programming

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

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

Overview of Java Threads (Part 2)

Multithreaded Programming

Concurrent Programming using Threads

Performance Throughput Utilization of system resources

CMSC 330: Organization of Programming Languages

Unit III Rupali Sherekar 2017

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

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

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

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

CIS233J Java Programming II. Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

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

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

CS360 Lecture 10 Multithreading

Chapter 32 Multithreading and Parallel Programming

Robotics and Autonomous Systems

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Multithread Computing

G52CON: Concepts of Concurrency

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

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

Threads Questions Important Questions

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

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

Definition: A thread is a single sequential flow of control within a program.

Software Practice 1 - Multithreading

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

Multithreading using Java. Dr. Ferdin Joe John Joseph

Basics of. Multithreading in Java

Threads, Concurrency, and Parallelism

What is a thread anyway?

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

G52CON: Concepts of Concurrency

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

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

Module - 4 Multi-Threaded Programming

Concurrent Programming

Unit 5 - Exception Handling & Multithreaded

Advanced Programming Concurrency

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

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

Programming Language Concepts: Lecture 11

CS11 Java. Fall Lecture 7

Java Programming Lecture 23

Multi-threading in Java. Jeff HUANG

CSCD 330 Network Programming

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

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Amity School of Engineering

CSCD 330 Network Programming

Parallel Programming Practice

Program #3 - Airport Simulation

Advanced Object-oriented Programming

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CERTIFICATION OBJECTIVES

Parallel Programming Practice

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

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

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

Java Threads. Thread. Rui Moreira. control within a program. threads. performed concurrently

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Transcription:

G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Natasha Alechina School of Computer Science nza@cs.nott.ac.uk

Outline of this lecture Java implementations of concurrency process and threads a simple ParticleApplet example ways of creating Thread objects in Java: extending the Thread class; and implementing the Runnable interface the lifecycle of a Thread: starting a new Thread, while the Thread is running; and shutting it down 2

Implementations of Concurrency We can distinguish two main types of implementations of concurrency: shared memory: the execution of concurrent processes by running them on one or more processors all of which access a shared memory processes communicate by reading and writing shared memory locations; and distributed processing: the execution of concurrent processes by running them on separate processors which don t share memory processes communicate by message passing. 3

Java Implementations of Concurrency Java supports both shared memory and distributed processing implementations of concurrency: shared memory: multiple user threads in a single Java Virtual Machine threads communicate by reading and writing shared memory locations; and distributed processing: via the java.net and java.rmi packages threads in different JVMs communicate by message passing or (remote procedure call) G52CON covers both approaches 4

Processes and Threads A process is any thread of execution or control, e.g.: part of a concurrent program (lightweight process) programs running in different address spaces on the same processor (heavyweight or OS processes) running on a different processor or on a different computer A thread is a process which forms part of a concurrent program threads execute within a shared address space a Java thread is a process running within a JVM 5

Threads in Java A thread is a single sequential flow of control within a Java program. Thread A Thread B Within the JVM, the threads comprising a Java program are represented by instances of the Thread class. 6

Thread lifecycle wait notify yield alive New Thread start Running Runnable return sleep Not runnable timeout I/O completes blocked on I/O Dead 7

A Simple Example: ParticleApplet (Brian Logan) ParticleApplet creates n Particle objects, sets each particle in autonomous continuous motion, and periodically updates the display to show their current positions: each Particle runs in its own Java Thread which computes the position of the particle; and an additional ParticleCanvas Thread periodically checks the positions of the particles and draws them on the screen. in this example there are at least 12 threads and possibly more, depending on how the browser handles applets. 8

ParticleApplet There are three classes: Particle: represents the position and behaviour of a particle and can draw the particle at its current position; ParticleCanvas: provides a drawing area for the Particles, and periodically asks the Particles to draw themselves; and ParticleApplet: creates the Particles and the canvas and sets the Particles in motion. See also Lea (2000), chapter 1 for an alternative implementation. 9

Thread lifecycle: creation wait notify yield alive New Thread start Running Runnable return sleep Not runnable timeout I/O completes blocked on I/O Dead 10

Creating Threads There are two ways to create a thread: extending the Thread class and overriding its run() method; or defining a class which implements the Runnable interface and its run() method public interface java.lang.runnable { void run(); and passing the Runnable object to the Thread constructor. The Thread class implements the Runnable interface. 11

Extending the Thread class class Particle extends Thread { protected int x, y; protected final random rng = new Random(this.hashCode()); // constructor etc public void run() { try { for(;;) { move(); sleep(100); catch (InterruptedException e) { return; // other methods... 12

Particle class continued public synchronized void move() { x += (rng.nextint() % 10); y += (rng.nextint() % 10); public void draw(graphics g) { int lx, ly; synchronized(this) { lx = x; ly = y; g.drawrect(lx, ly, 10, 10); 13

Implementing Runnable class ParticleCanvas extends Canvas implements Runnable { private Particle[] particles = new Particle[0]; // constructor etc... public void run() { try { for(;;) { repaint(); Thread.sleep(100); catch (InterruptedException e) { return; // other methods... 14

ParticleCanvas class continued protected synchronized void getparticles() { return particles; // called by Canvas.repaint(); public void paint(graphics g) { Particle[] ps = getparticles(); for (int i = 0; i < ps.length(); i++) ps[i].draw(g); 15

Particle threads public class ParticleAppletA extends Applet { protected final ParticleCanvas canvas = new ParticleCanvas(400); protected Particle[] particles; // null when not running protected Thread canvasthread; // ParticleApplet start method public synchronized void start() { int n = 10; // just for demo if (particles == null) { // bypass if already started particles = new Particle[n]; for (int i = 0; i < n; ++i) { particles[i] = new Particle(200, 200); particles[i].setname("particle Thread " + i); particles[i].start(); canvas.setparticles(particles); // continued... 16

ParticleCanvas thread public class ParticleAppletA extends Applet { protected final ParticleCanvas canvas = new ParticleCanvas(400); protected Particle[] particles; // null when not running protected Thread canvasthread; // ParticleApplet start method... public synchronized void start() { int n = 10; // just for demo if (particles == null) { // bypass if already started // code to start particles omitted canvasthread = new Thread(canvas); canvasthread.setname("canvas Thread"); canvasthread.start(); 17

Thread lifecycle: running wait notify yield alive New Thread start Running Runnable return sleep Not runnable timeout I/O completes blocked on I/O Dead 18

Particle.run() class Particle extends Thread { // fields, constructor etc public void run() { try { for(;;) { move(); sleep(100); catch (InterruptedException e) { return; // other methods 19

Particle threads move(); sleep(100); move(); sleep(100); move(); sleep(100); particles[0] particles[1]... particles[9] 20

ParticleCanvas.run() class ParticleCanvas extends Canvas implements Runnable { // fields, constructor etc public void run() { try { for(;;) { repaint(); Thread.sleep(100); catch (InterruptedException e) { return; // other methods 21

ParticleCanvas thread move(); sleep(100); move(); sleep(100); repaint(); particles[0].draw particles[1].draw... particles[9].draw Thread.sleep(100); move(); sleep(100); particles[0] particles[1]... particles[9] 22

Thread lifecycle: not runnable wait notify yield alive New Thread start Running Runnable return sleep Not runnable timeout I/O completes blocked on I/O Dead 23

The not runnable state A running Thread becomes not runnable when: it calls sleep() to tell the scheduler that it no longer wants to run; it blocks for I/O; or it blocks in wait()for condition synchronisation. 24

Examples of not runnable Particle threads become not runnable when they sleep() the ParticleCanvas thread becomes not runnable when it calls repaint() we ll return to wait() and condition synchronisation in later lectures 25

Scheduling methods The Thread class provides the following static scheduling methods: sleep(long msecs): causes the current thread to suspend for at least msecs milliseconds. yield(): requests that the JVM to run any other runnable but nonrunning thread rather than the current thread. 26

Thread priorities Threads have priorities which heuristically influence schedulers: each thread has a priority in the range Thread.MIN_PRIORITY to Thread.MAX_PRIORITY by default, each new thread has the same priority as the thread that created it---the initial thread associated with a main method by default has priority Thread.NORM_PRIORITY the current priority of a thread can be accessed by the method getpriority and set via the method setpriority. When there are more runnable threads than CPUs, a scheduler is generally biased in favour of threads with higher priorities. 27

Thread lifecycle: cancellation wait notify yield alive New Thread start Running Runnable return sleep Not runnable timeout I/O completes blocked on I/O Dead 28

Thread termination A thread terminates when its run() method completes: either by returning normally; or by throwing an unchecked exception (RuntimeException, Error or one of their subclasses) Threads are not restartable invoking start() more than once results in an InvalidThreadStateException. 29

Thread cancellation There are several ways to get a thread to stop: when the thread s run() method returns; call Thread.stop() --- this is a bad idea, as it doesn t allow the thread to clean up before it dies; or interrupt() the thread. A multi-threaded program will continue to run until its last (non-daemon) thread terminates. 30

Interrupting a Thread Each Thread object has an associated boolean interruption status: interrupt(): sets a running thread s interrupted status to true isinterrupted(): returns true if the thread has been interrupted by interrupt() A thread can periodically check its interrupted status, and if it is true, clean up and exit. 31

Thread (checked) exceptions Threads which are blocked in calls wait() and sleep() aren t runnable, and can t check the value of the interrupted flag interrupting a thread which is waiting or sleeping aborts the thread and throws an InterruptedException. synchronized <method or block> try { wait() sleep() catch (InterruptedException e) { // clean up and return 32

Stopping the ParticleApplet // ParticleApplet stop method (not Thread.stop) public synchronized void stop() { // Bypass if already stopped if (particles!= null) { for (int i = 0; i < particles.length; ++i) particles[i].interrupt(); particles = null; canvasthread.interrupt(); canvasthread = null; 33

Stopping the Particles // Particle run method public void run() { try { for(;;) { move(); sleep(100); catch (InterruptedException e) { return; 34

The Next Lecture Suggested reading: Synchronisation Andrews (2000), chapter 2, sections 2.1, chapter 3, section 3.1; Ben-Ari (1982), chapter 2. 35