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

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Threads  A thread is single sequence of executable statements within a program  eg.) a typical application is a thread, with the flow of control beginning.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Definitions Process – An executing program
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Concurrent Programming and Threads Threads Blocking a User Interface.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Java Thread Programming
Principles of Software Development
Java Multithreading.
Threads and Multithreading
CSE 501N Fall ‘09 21: Introduction to Multithreading
Chapter 19 Java Never Ends
Threads Chate Patanothai.
CS18000: Problem Solving and Object-Oriented Programming
Threads and Multithreading
Principles of Software Development
Multithreading.
Threads and Multithreading
Threads and Multithreading
21 Threads.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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 } 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

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 } 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 lines of output

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 }

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 }

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

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 } 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

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

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

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

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