concurrency in javapsnyder/cs342-summer2017/... · java's concurrency model? a. two threads...

44
Concurrency in Java July 10, 2017

Upload: others

Post on 19-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Concurrency in JavaJuly 10, 2017

Page 2: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Reading Quiz

Page 3: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

What is Concurrency?A. Execute multiple programs (or parts of programs) at the same time.

B. Making a single program run faster through pipelining.

C. Reducing memory use by compressing data automatically.

D. Writing a single program in multiple programming languages.

Page 4: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Which Java code is not related to concurrency?

A. new java.lang.Thread(() -> {// fake out};)

B. public synchronized void yeppers() {};

C. catch (InterruptedException error) { }

D. public final class PikaPika {}

Page 5: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

What is a risk associated with Java's concurrency model?

A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown.

B. Compatibility problems, due to changes in the Java language over time.

C. Two threads may access the same variable in an unpredictable way, leading to correctness issues.

D. Java's concurrency model is very verbose, requiring a lot of typing, making it likely that code will include errors.

Page 6: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

What does the "synchronize" modifier for?

A.To try and speed up concurrent programs by batching processor instructions.

B.To try and make Java less fun by requiring more typing.

C.To try and make concurrent programs more memory efficient by sharing resources between threads.

D.To try and prevent integrity errors related to concurrency.

Page 7: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

What in the world is being demonstrated in this image from the book?

A. That threads share some kinds of memory, but not others.

B. That Java is great for drawing arrows.

C. How java manages tables in memory.

D. That some threads are of type Tenured, some are of type Survivor, and some are of type Eden.

Page 8: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Homework 5

Page 9: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Homework 5

• Due in a week

• Unit testing / Test driven development

• We'll discuss after the break

Page 10: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Final Project

Page 11: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Final Project• Due July 31st

• Groups of one or two people

• Email me groups by class on Wednesday

• If you want a group, but can't find one, email me

• I'll meet with groups on Wednesday

Page 12: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Final Projects• Patch to open source project

• Select your own project, or I can suggest one

• Must be code

• Really really good for resume!

• Some non-trivial java application

• Web application

• Android application (lots of work!)

• Fine, but not as good, for a resume

Page 13: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Part One

• Status checks in class with me every other class

• One half to one-page summary of what has been accomplished since previous meeting (specific specific specific!)

• Graded, not just instructive

Page 14: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Part Two

• Ten to fifteen minute class presentation

• Introduce the project

• Programming challenges

• What you learned / would recommend to others

Page 15: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Part Three• Code / contribution evaluation

• Code quality

• Documentation

• Unit tests

• Communication with project managers (if applicable)

Page 16: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Concurrency

Page 17: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Problem

• We want our programs to go fast

• Processors used to get much faster, quickly

• Less and less the case

Page 18: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Processor Speeds over Time

Page 19: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

The Speed Problem

• Instead of getting faster, processors now get more complex

• Execute multiple things at the same time

• Performance improvement through waiting less

Page 20: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

main()

difficultOne()

difficultTwo()

difficultThree()

return;

Page 21: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

problem/Main.java –>

Page 22: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Parallel Execution

• Subprocesses

• Threads

• Runtime managed threads (ie threads, with Java doing the hard stuff)

Page 23: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Subprocesses

Page 24: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Subprocesses

• Write programs that do smaller parts of the task

• Run these child programs (processes) from our program

• Read the results back into our program

• No shared memory

Page 25: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

main()

difficultOne()

difficultTwo()

difficultThree()

return;

Page 26: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

main() main() main()

main()

return;

difficultOne() difficultTwo() difficultThree()

Page 27: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Subprocesses in Java• ProcessBuilder

Manage connections to subprocesses

- ProcessBuilder#start: Start running the child process

• Process Represents each child process

- Process#waitFor: Wait until the process finishes

• InputStreamReader Read output from child process

Page 28: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

subprocess/{Main, Subprocess}.java –>

Page 29: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Subprocess Benefits

• Simple!

• Cross language (subprocess/Main2.java –>)

• Free of all the problems of threading (deadlocks, race conditions, etc.)

Page 30: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Subprocess Costs

• Expensive (kernel has to manage each process)

• Sharing data is difficult (STDIO, STDIN, pipes)

• Slower than alternatives

Page 31: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Threads

Page 32: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Threads

• "Sub-program" / multiple executions within a program

• Share memory across "sub-programs"

• Instruct java which parts of our program can run at the same time (threads)

Page 33: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

main()

difficultOne()

difficultTwo()

difficultThree()

return;

Page 34: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

main()

return;

difficultOne() difficultTwo() difficultThree()

Page 35: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Threading in Java• Thread

Represents a thread of execution

- Thread::sleep Tells the current thread to pause for a while

- Thread#startStarts the constructed thread's execution

- Thread#joinWait for a thread to finish executing

• Runnable Interface that represents something that can be run as a thread

Page 36: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

threads/Main.java –>

Page 37: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Threading Benefits

• Fast!

• Share memory (no pushing stuff between child processes, etc.)

• Granular / low memory cost (compared to processes)

Page 38: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Threading Costs

• Extremely easy to get wrong

• Deadlocks

• Race conditions

• REALLY REALLY REALLY easy to get wrong

Page 39: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown
Page 40: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

threads/Trouble.java –>

Page 41: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

threads/WhatWentWrong.java –>

Page 42: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown
Page 43: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Page 44: Concurrency in Javapsnyder/cs342-summer2017/... · Java's concurrency model? A. Two threads may try to access the same memory at the same time, leading to an Exception being thrown

Homework 5

• Online at https://www.cs.uic.edu/~psnyder/cs342-summer2017/homework/hw5.html

• Examples of problem cases?