Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

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

2 Outline USC CSCI 201L2/22 ▪T▪Threads ▪M▪Multi-Threaded Code ▪C▪CPU Scheduling ▪P▪Program

3 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/22 Threads

4 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 201L4/22 Threads

5 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 201L5/22 Threads

6 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 201L6/22 Threads

7 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 201L7/22 Threads

8 Thread States USC CSCI 201L8/22 Threads Start Ready Running WaitingSleeping Dead

9 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 201L9/22 Threads

10 Outline USC CSCI 201L10/22 ▪T▪Threads ▪M▪Multi-Threaded Code ▪C▪CPU Scheduling ▪P▪Program

11 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 201L11/22 Multi-Threaded Code

12 Thread Class USC CSCI 201L12/22 Multi-Threaded Code

13 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 201L13/22 Multi-Threaded Code These are the first 22 lines of output

14 Multi-Threaded Example - Runnable 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 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 201L14/22 Multi-Threaded Code These are the last 20-21 lines of output

15 Multi-Threaded Example #2 1 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 201L15/22 Multi-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 }

16 Multi-Threaded Example #2 1 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 201L16/22 Multi-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 }

17 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 201L17/22 Multi-Threaded Code

18 sleep() Example 1 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 201L18/22 Multi-Threaded Code This code outputs similarly to the previous example, but all of the values will be printed one second apart from each other

19 Outline USC CSCI 201L19/22 ▪T▪Threads ▪M▪Multi-Threaded Code ▪C▪CPU Scheduling ▪P▪Program

20 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 201L20/22 CPU Scheduling

21 Outline USC CSCI 201L21/22 ▪T▪Threads ▪M▪Multi-Threaded Code ▪C▪CPU Scheduling ▪P▪Program

22 Program ▪Write a multi-threaded program that flashes “Welcome” in a GUI every 0.5 seconds using the Graphics object. USC CSCI 201L22/22 Program


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

Similar presentations


Ads by Google