22
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/ ~CSCI201 USC CSCI 201L

Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Embed Size (px)

Citation preview

Page 1: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Concurrent ComputingCSCI 201L

Jeffrey Miller, Ph.D.

HTTP://WWW-SCF.USC.EDU/~CSCI201

USC CSCI 201L

Page 2: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 2/22

▪ Threads▪ Multi-Threaded Code▪ CPU Scheduling▪ Program

Page 3: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Thread Overview

▪ Looking at the Windows taskbar, you will notice more than one process running concurrently

▪ Looking at the Windows task manager, you’ll see a number of more processes running

▪ But how many processes are actually

running at any one time?

USC CSCI 201L 3/22Threads

Page 4: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Programs vs Processes vs Threads

▪ Programs› An executable file residing in memory, typically in secondary storage

▪ Processes› An executing instance of a program› Sometimes this is referred to as a Task

▪ Threads› Any section of code within a process that can execute at what appears to be

simultaneously› Shares the same resources allotted to the process by the OS kernel

USC CSCI 201L 4/22Threads

Page 5: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Concurrent Programming

▪ Concurrent Programming› Multiple sections of a process or multiple processes

executing at what appears to be simultaneously

▪ Parallel Programming› Multiple processes or multiple sections of a process

executing simultaneously in different cores of a CPU or different CPUs

▪ Distributed Programming› Multiple processes or multiple sections of a process

executing on multiple CPUs that are located on different computers

USC CSCI 201L 5/22Threads

Page 6: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Examples of Concurrent Programming

▪ Chatting› While being able to type and send a chat message, the

program can also receive a chat message

▪ Message Updates› In social media applications, while you are reading other

posts, an update can come in to notify you that a new post has been made

▪ Multi-player Games› There are many different actions that are happening with

different characters in a multi-player game. Each character is typically operating in its own thread

USC CSCI 201L 6/22Threads

Page 7: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Thread Details

▪ The operating system will switch threads into and out of the CPU› This is called time slicing

▪ Time slicing is efficient because the CPU sits idle a lot of the time› For example, waiting for user input or when the user is not

interacting with a program

▪ All Java programs already have one thread, which is the thread that contains the main method› We will learn how to create multiple threads to run within

our program here

USC CSCI 201L 7/22Threads

Page 8: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Thread States

USC CSCI 201L 8/22Threads

Start

Ready

Running

Waiting Sleeping

Dead

Page 9: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Threaded Debugging

▪ Since we have no control over when a program will be switched into and out of the CPU, there are many different executions of a program

▪ If you see an error during one execution, you may not see the same error during the next execution› This does not mean that the code is not deterministic, but

rather that there are many different paths of execution

USC CSCI 201L 9/22Threads

Page 10: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 10/22

▪ Threads▪ Multi-Threaded Code▪ CPU Scheduling▪ Program

Page 11: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Creating a Thread

▪ Creating multi-threaded code in Java is rather easy› Implement the Runnable interface, pass it into an instance of a

Thread, and start the thread• Runnable is in java.lang, so no additional imports are needed

▪ There is a second way to create multi-threaded code in Java› Extend the Thread class (which implements the Runnable

interface), and start the thread• Thread is in java.lang, so no additional imports are needed

▪ In either of the above cases› Call the start() method on the Thread class to kick off the new

thread› Override the public void run() method in your new class

• The run() method will be called when start() is called on your thread• What happens if you just call run() yourself instead of calling start()

USC CSCI 201L 11/22Multi-Threaded Code

Page 12: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Thread Class

USC CSCI 201L 12/22Multi-Threaded Code

Page 13: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Threaded Example - Thread

1 public class Test {2 public static void main(String[] args ) {3 System.out.println("First line");4 for (int i=0; i < 100; i++) {5 TestThread tt = new TestThread(i);6 tt.start();7 }8 System.out.println("Last line");9 }10 }11 12 class TestThread extends Thread {13 private int num;14 public TestThread(int num) {15 this.num = num;16 }17 public void run() {18 System.out.println(num + "Hello");19 System.out.println(num + "CSCI 201L");20 }21 }

USC CSCI 201L 13/22Multi-Threaded Code

These are the first 22 lines of output

Page 14: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Threaded Example - Runnable1 public class Test {2 public static void main(String[] args ) {3 System.out.println("First line");4 for (int i=0; i < 100; i++) {5 TestThread tt = new TestThread(i);6 Thread t = new Thread(tt);7 t.start();8 }9 System.out.println("Last line");10 }11 }12 13 class TestThread implements Runnable {14 private int num;15 public TestThread(int num) {16 this.num = num;17 }18 public void run() {19 System.out.println(num + "Hello");20 System.out.println(num + "CSCI 201L");21 }22 }

USC CSCI 201L 14/22Multi-Threaded Code

These are the last 20-21 lines of output

Page 15: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Threaded Example #21 public class Test {2 public static void main(String[] args ) {3 System.out.println("First line");4 TestThread ta = new TestThread('a');5 TestThread tb = new TestThread('b');6 TestThread tc = new TestThread('c');7 ta.start();8 tb.start();9 tc.start();10 System.out.println("Last line");11 }12 }13

USC CSCI 201L 15/22Multi-Threaded Code

14 class TestThread extends Thread {15 private char c;16 public TestThread(char c) {17 this.c = c;18 }19 public void run() {20 for (int i=0; i < 20; i++) {21 System.out.print(i + "" + c + " ");22 }23 System.out.println("");24 }25 }

Page 16: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Threaded Example #21 public class Test {2 public static void main(String[] args ) {3 System.out.println("First line");4 TestThread ta = new TestThread('a');5 TestThread tb = new TestThread('b');6 TestThread tc = new TestThread('c');7 ta.start();8 tb.start();9 tc.start();10 System.out.println("Last line");11 }12 }13

USC CSCI 201L 16/22Multi-Threaded Code

14 class TestThread extends Thread {15 private char c;16 public TestThread(char c) {17 this.c = c;18 }19 public void run() {20 for (int i=0; i < 20; i++) {21 System.out.print(i + "" + c + " ");22 }23 System.out.println("");24 }25 }

Page 17: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Thread.sleep(long) method

▪ The sleep(long milliseconds) method in the Thread class puts the thread to sleep for a number of milliseconds› This allows other threads to be switched into the CPU

▪ The sleep method does not cause the thread to be immediately switched back into the CPU after the number of milliseconds› From the sleeping state, the thread will go back into the ready state› This means that the thread will sleep for at least that many

milliseconds

▪ Although rare, the sleep method can throw an InterruptedException› An InterruptedException gets called when a thread’s interrupt() method

is called› The interrupt() method would get called when another thread wants

that thread to stop whatever it is doing immediately

USC CSCI 201L 17/22Multi-Threaded Code

Page 18: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

sleep() Example1 public class Test {2 public static void main(String[] args ) {3 System.out.println("First line");4 TestThread ta = new TestThread('a');5 TestThread tb = new TestThread('b');6 TestThread tc = new TestThread('c');7 ta.start();8 tb.start();9 tc.start();10 System.out.println("Last line");11 }12 }13 14 class TestThread extends Thread {15 private char c;16 public TestThread(char c) {17 this.c = c;18 }19 public void run() {20 for (int i=0; i < 20; i++) {21 System.out.print(i + "" + c + " ");22 try {23 Thread.sleep(1000);24 } catch (InterruptedException ie) {25 System.out.println("interrupted");26 return;27 }28 }29 System.out.println("");30 }31 }

USC CSCI 201L 18/22Multi-Threaded Code

This code outputs similarly to the previous example, but all of the values will be printed one second apart from each other

Page 19: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 19/22

▪ Threads▪ Multi-Threaded Code▪ CPU Scheduling▪ Program

Page 20: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

CPU Scheduling Overview

▪ The operating system will schedule different processes to execute in the CPU at different times

▪ Each process will execute for a given amount of time (or less if the entire time is not needed), then be switched out of the CPU so other processes don’t starve

▪ The algorithm by which an operating system performs context switches is specific to the OS, though it can include priorities, round robin, fixed time, and many others

▪ It is important to note that an application programmer cannot determine when a process will be switched into the CPU but can only notify the operating system that the program is ready to be executed

USC CSCI 201L 20/22CPU Scheduling

Page 21: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 21/22

▪ Threads▪ Multi-Threaded Code▪ CPU Scheduling▪ Program

Page 22: Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Program

▪ Write a multi-threaded program that flashes “Welcome” in a GUI every 0.5 seconds using the Graphics object.

USC CSCI 201L 22/22Program