Michele Van Dyne MUS 204B https://katie.mtech.edu/classes/csci136. Threads

Similar documents
Threads. Fundamentals of Computer Science

Michele Van Dyne MUS 204B Concurrency Issues

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Concurrency. Fundamentals of Computer Science

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Concurrent Computing CSCI 201 Principles of Software Development

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

Threads Chate Patanothai

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

7. MULTITHREDED PROGRAMMING

Single processor CPU. Memory I/O

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Multiprocessing Threads/Lightweight objects Thread methods Concurrency Issues

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

CS 351 Design of Large Programs Threads and Concurrency

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

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

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

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

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

/** * Created Aug 19, 2012 */ package com.roguelogic.util;

Animation Part 2: MoveableShape interface & Multithreading

Multiprocessing Threads/Lightweight objects Thread methods Concurrency Issues

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

Exercise Session Week 8

Multithreaded Programming

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

COE518 Lecture Notes Week 7 (Oct 17, 2011)

GRAPHICS AND SOUND. Fundamentals of Computer Science I

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

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

Concurrency and Java Programming

Exercise Session Week 8

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

Principles of Software Construction: Concurrency, Part 1

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

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

CS 556 Distributed Systems

CS18000: Programming I

JAVA - MULTITHREADING

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

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

CS360 Lecture 12 Multithreading

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Concurrent Programming: Threads. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CSCD 330 Network Programming

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

The Java Programming Language Basics. Identifiers, Keywords, and Types. Object-Oriented Programming. Objects and Classes. Exception Handling

Multithreaded Programming

Stacks and Queues. Fundamentals of Computer Science.

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

Synchronization synchronization.

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Principles of Software Construction: Concurrency, Part 1

Parallel Programming Languages COMP360

Multithread Computing

CSCD 330 Network Programming

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

Stacks and queues. CSCI 135: Fundamentals of Computer Science Keith Vertanen.

Threads Assistant Professor DCS Operating System Concepts

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

Software Practice 1 - Multithreading

Java FX. Threads, Workers and Tasks

Multithreading Pearson Education, Inc. All rights reserved.

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Program #3 - Airport Simulation

/* Copyright 2012 Robert C. Ilardi

User Space Multithreading. Computer Science, University of Warwick

The Java Programming Language Basics. Identifiers, Keywords, and Types. Object-Oriented Programming. Objects and Classes. Exception Handling

Chapter 8 Threads Zindell Technologies, Ltd. Question 1: Which one statement below is true concerning the following code?

04-Java Multithreading

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

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

Object Oriented Programming. Week 10 Part 1 Threads

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

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

Synchronization in Java

Lecture 35. Threads. Reading for next time: Big Java What is a Thread?

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Java Threads. 02/05/2007 CSCI 315 Operating Systems Design 1

Internet Technology 2/7/2013

Java Threads. What Are Threads? General-purpose solution for managing concurrency. Multiple independent execution streams. Shared state.

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

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

Introduction to Java Threads

Multithreading. A thread is a unit of control (stream of instructions) within a process.

TDDB84: Lecture 5. Singleton, Builder, Proxy, Mediator. fredag 27 september 13

THREADS AND CONCURRENCY

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

ECE Fall 2014, Final Exam

Question Points Score Total 100

Input and output thus far. Graphics and sound Input. New input/output capabilities. Output. StdDraw overview. StdDraw. StdAudio

Advanced Programming C# Lecture 13. dr inż. Małgorzata Janik

Exam Number/Code : 1Z Exam Name: Name: Java Standard Edition 6. Demo. Version : Programmer Certified Professional Exam.

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

CS370 Operating Systems

Transcription:

Michele Van Dyne MUS 204B mvandyne@mtech.edu https://katie.mtech.edu/classes/csci136 Threads

Multi-threaded programs Multiple simultaneous paths of execution Seemingly at once (single core) Actually at the same time (multiple cores) Why? Get work done faster (on multi-core machines) Simplify code by splitting up work into parts Remain responsive to user input Threads in Java Creating, starting, sleeping Unpredictability Debugging

What Andy giveth, Bill taketh away. http://www.gotw.ca/publications/concurrency-ddj.htm 3

public class Animal private String image; private String audio; public static void main(string [] args) 1 HashMap<String, Animal> map = new HashMap<String, Animal>(); 9 A 6 7 public Animal(String image, String audio) 3 this.image = image; 4 this.audio = audio; public void show() H StdDraw.picture(0.5, 0.5, image); I StdAudio.play(audio); Animal.java 2 5 8 B map.put("dog", new Animal("dog.jpg", "dog.wav")); map.put("cat", new Animal("cat.jpg", "cat.wav")); map.put("cow", new Animal("cow.jpg", "cow.wav")); while (true) C D E F G J StdDraw.clear(); String name = StdIn.readLine(); Animal animal = map.get(name.tolowercase().trim()); if (animal!= null) animal.show(); StdDraw.show(100); AnimalMap.java A single core computer. 4

public class Animal private String image; private String audio; public static void main(string [] args) 1 HashMap<String, Animal> map = new HashMap<String, Animal>(); 9 A 6 7 public Animal(String image, String audio) 3 this.image = image; 4 this.audio = audio; public void show() H StdDraw.picture(0.5, 0.5, image); I StdAudio.play(audio); Animal.java 2 5 8 B map.put("dog", new Animal("dog.jpg", "dog.wav")); map.put("cat", new Animal("cat.jpg", "cat.wav")); map.put("cow", new Animal("cow.jpg", "cow.wav")); while (true) C D E F G J StdDraw.clear(); String name = StdIn.readLine(); Animal animal = map.get(name.tolowercase().trim()); if (animal!= null) animal.show(); StdDraw.show(100); AnimalMap.java A multi-core computer. 5

New magic program: AnimalMapDeluxe.java Random spinning frogs! Frogs appear every second User can make requests: "dog", "cat", "cow" Even while frog is spinning 6

Thread A separate path of execution Also: the name of a class in the Java API Program creates an object of type Thread Creating and starting a thread: Thread thread = new Thread(); thread.start(); Simple, but doesn't actually do anything: Thread is born Thread dies End of story 7

Thread constructor can take an object Object must: implements Runnable Interface with a single method: run() run() may do work forever (e.g. random spinning frogs) run() may do something and exit (e.g. print a message) run() can call other methods, create objects, public class BlastOff implements Runnable public void run() for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); 8

public class BlastOff implements Runnable public void run() 9... 7 8 5 6 for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); public class Launch public static void main(string [] args) 1 2 3 4 System.out.println("prepare for launch"); Thread thread = new Thread(new BlastOff()); thread.start(); System.out.println("done with launch"); % java Launch prepare for launch done with launch 10 9 8 7 6 5 4 3 2 1 BLAST OFF! 9

public class BlastOff implements Runnable public void run() 9... 7 8 4 6 for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); public class Launch public static void main(string [] args) 1 2 3 5 System.out.println("prepare for launch"); Thread thread = new Thread(new BlastOff()); thread.start(); System.out.println("done with launch"); % java Launch prepare for launch done with launch 10 9 8 7 6 5 4 3 2 1 BLAST OFF! Different execution order: same output 10

public class BlastOff implements Runnable public void run() 9... 7 8 4 5 for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); public class Launch public static void main(string [] args) 1 2 3 6 System.out.println("prepare for launch"); Thread thread = new Thread(new BlastOff()); thread.start(); System.out.println("done with launch"); % java Launch prepare for launch 10 done with launch 9 8 7 6 5 4 3 2 1 BLAST OFF! Different execution order: different output 11

public class BlastOff implements Runnable public void run() for (int i = 10; i > 0; i--) System.out.print(i + " "); System.out.println("BLAST OFF!"); public class Launch public static void main(string [] args) 1 2 3 8 9... 6 7 4 5 System.out.println("prepare for launch"); Thread thread = new Thread(new BlastOff()); thread.start(); System.out.println("done with launch"); Lots more possible execution orders! Exact order depends on: thread scheduler % java Launch prepare for launch 10 9 done with launch 8 7 6 5 4 3 2 1 BLAST OFF! 12

Thread t = new Thread(r); Just a normal object on the heap t.start(); Waiting to be selected by the thread scheduler Actually running on the CPU 13

14

Main program: launch > 1 threads Split work into multiple parts public class MultiLaunch public static void main(string [] args) final int N = Integer.parseInt(args[0]); System.out.println("prepare for multi launch"); Thread [] threads = new Thread[N]; for (int i = 0; i < N; i++) threads[i] = new Thread(new BlastOff()); threads[i].start(); System.out.println("done launching"); % java MultiLaunch 3 prepare for multi launch 10 10 10 done launching 9 9 9 8 8 7 8 6 7 5 7 4 6 3 6 2 5 1 5 BLAST OFF! 4 4 3 3 2 2 1 BLAST OFF! 1 BLAST OFF! 15

Main thread can wait for workers e.g. Merge results from workers Call join() on the thread object Alice Bob Carl Passing data to/from a worker Thread() constructor is passed a Runnable object Add any instance variables / methods you want Input: send as parameters to constructor Output: add some get data method(s) But: you must keep track of object sent to Thread() 16

public class FibWorker implements Runnable private int num = 0; private long result = 0; public FibWorker(int num) this.num = num; public long getresult() return result; public void run() result = fib(num); private long fib(int n) if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); Get the input, what we'll calculate once somebody says go! Somebody getting the output. Should only be called after the thread is known to be done. Method that runs when somebody call.start() on a Thread that has been passed this object. 17

public class FibLauncher public static void main(string [] args) Thread [] threads = new Thread[args.length]; FibWorker [] workers = new FibWorker[args.length]; for (int i = 0; i < args.length; i++) workers[i] = new FibWorker(Integer.parseInt(args[i])); threads[i] = new Thread(workers[i]); threads[i].start(); try for (int i = 0; i < args.length; i++) threads[i].join(); System.out.print("fib(" + args[i] + ") = "); System.out.println(workers[i].getResult()); catch (InterruptedException e) e.printstacktrace(); We must keep track of two parallel arrays, one for threads and one for worker objects. Setup worker with its job. Once a thread is done, we know it has a good output value. 18

Making a thread take a nap Specify nap time in milliseconds Guaranteed to sleep at least this long Maybe longer though Allows other threads to enter running state Polite behavior when you've got nothing to do Thread.sleep(ms), but must catch an exception while (!StdDraw.hasNextKeyTyped()) // Burns an entire core to do nothing! char ch = StdDraw.nextKeyTyped(); while (!StdDraw.hasNextKeyTyped()) try Thread.sleep(10); catch (InterruptedException e) e.printstacktrace(); char ch = StdDraw.nextKeyTyped(); 19

Threads can be given a name Helpful for debugging Second parameter to Thread() constructor Alice Bob Carl public class MultiLaunchSleep public static void main(string [] args) final int N = Integer.parseInt(args[0]); System.out.println("prepare for multi launch"); Thread [] threads = new Thread[N]; for (int i = 0; i < N; i++) threads[i] = new Thread(new BlastOff(), "B" + i); threads[i].start(); System.out.println("done launching"); 20

public class BlastOffSleep implements Runnable public void run() String myname = Thread.currentThread().getName(); for (int i = 10; i > 0; i--) System.out.print(i + "(" + myname + ") "); try Thread.sleep(1000); catch (InterruptedException e) e.printstacktrace(); System.out.println("BLAST OFF! (" + myname + ")"); % java MultiLaunchSleep 3 prepare for multi launch 10(B0) done launching 10(B2) 10(B1) 9(B0) 9(B1) 9(B2) 8(B0) 8(B1) 8(B2) 7(B0) 7(B1) 7(B2) 6(B1) 6(B0) 6(B2) 5(B0) 5(B2) 5(B1) 4(B0) 4(B1) 4(B2) 3(B0) 3(B1) 3(B2) 2(B0) 2(B1) 2(B2) 1(B0) 1(B1) 1(B2) BLAST OFF! (B0) BLAST OFF! (B1) BLAST OFF! (B2) 21

Java Thread Multiple simultaneous paths of execution Really simultaneous (multiple cores) Simply seem that way (single core) Important thread skills: Implementing a worker class Starting a thread Making a thread sleep Waiting for a thread to finish Getting input to a thread Getting output from a thread