ON BEYOND OBJECTS PROGRAMMING IN THE 21TH CENTURY COMP 590 059 FALL 2024 David Stotts Computer Science Dept UNC Chapel Hill COMP 301 Threads Review a Motivating example Baking cookies Baking cookies Measure the dry ingredient s Measure the wet ingredient s Combine the wet and dry ingredient s Put the cookie dough on the sheet Preheat the oven Bake the cookies progress of time Improving the algorithm Dry ingredients Wet ingredients Mix cookie dough Baking sheet Preheat oven Bake time Dry ingredients Wet ingredients Mix cookie dough Baking sheet Preheat oven Bake Make individual computation steps take less speed them up time Saved time You got a new mixer Now you can mix the ingredients faster Improving the algorithm Dry ingredients Wet ingredients Mix cookie dough Baking sheet Preheat oven Bake time Dry ingredients Wet ingredients Mix cookie dough Baking sheet Bake You can preheat the oven while mixing the ingredients Preheat oven Two steps overlap in time they do not affect each other so we do them at the same time Saved time Improving the algorithm time Dry ingredients Wet ingredients Mix cookie dough Baking sheet Bake Preheat oven You can have a friend help you Dry ingredients Mix cookie dough Baking sheet Bake Wet ingredients Preheat oven two separate bakers two computers working at the same time More Saved time How to speed up completing a task Strategy Baking cookies Executing a program Do the same steps only faster Upgrade your mixer Upgrade your computer Rearrange tasks so you can do one while waiting for the other to finish Preheat while portioning on the baking sheet Asynchronous programming Get a friend to help you do tasks at the same time Wet ingredients and dry ingredients at the same time Parallel programming Concurrent Computing Concurrent vs sequential computing Definitions Sequential computing When a series of computations are executed one at a time such that each computation must finish before the next can begin task1 task2 task3 Time Definitions Concurrent computing When a series of computations are executed during overlapping time periods task1 task2 task3 Time The tasks might be simultaneously executed or maybe one computer is taking turns switching back and forth between them Synchronous and asynchronous programming models Definitions Synchronous programming A model of programming where a task may be started and the program waits for it to complete before continuing on Task task method completed Main program task method executed Main program Time Definitions Asynchronous programming A model of programming where a task may be started but the program continues on without waiting for it to complete Task task method completed task method executed Main program Main program Time Summary Computing models Sequential Computing Only one task can be executed at a time the current task must complete before the next one can begin Concurrent Computing Multiple tasks can be executed during overlapping time periods either in parallel or by context switching Parallel Computing Multiple tasks can simultaneously be executed on separate processing elements Programming models Synchronous Programming The program waits for a task to finish completely before continuing on Asynchronous Programming The program continues on without waiting for a method to finish The Multicore Revolution 15 Core Count Driving Moore s Law https madusudanan com blog scala tutorials part 9 intro to functional programming Parallel Programming Multicore increases performance only if we can find ways to parallelize our task Sometimes very easy o No data or logic dependencies o Aggregating data operations arrays matrices Sometimes very hard 19 Threads and multithreading Definitions A thread is an abstraction for executing a program To execute any program your operating system creates a thread A thread encapsulates the following information 1 Instruction pointer the current point of execution 2 Call stack which methods are currently executing 3 Memory the contents of memory including the heap A single program can run multiple threads at the same time Separate instruction pointer Separate call stack Shared memory Java Process vs Thread program that is under execution is a process A process has one or more thread a heap for dynamic stuff memory for call stack Each thread has its own call stack its own private chunk of the process stack space shares the one heap in the process with all other threads medium co m Java Process vs Thread U is a o r e C P C medium co m Java Runnable interface The Runnable interface is built in to Java A Runnable object represents a task that can be performed The task is performed by calling the run method The task might be performed synchronously or asynchronously public interface Runnable void run What Java syntax shortcut might be used to create a new Runnable object Creating a Runnable object public class Multithreader public static void main String args Runnable task1 for int i 0 i 10 i System out println i 1 Alt No anonymous class no lambda public class Main public static void main String args Runnable task1 new AlgOne public class AlgOne implements Runnable public void run for int i 0 i 10 i System out println i 1 Running a Runnable object synchronously public class Multithreader public static void main String args Runnable task1 for int i 0 i 10 i System out println i 1 System out println Printing 1 to 10 task1 run System out println Done This program will execute sequentially and synchronously No concurrency here we simply created a Runnable object and executed its run method Alt No anonymous class no lambda public class Main public static void main String args Runnable task1 new AlgOne System out println Printing 1 to 10 task1 run System out println Done public class AlgOne implements Runnable public void run for int i 0 i 10 i System out println i 1 Java Thread class Java s Thread class Built in to Java Represents a thread of execution Must be given a Runnable object to execute in the constructor Has a special start method for executing the Runnable Executes the Runnable asynchronously in parallel Thread thread new Thread task1 thread start Running a Runnable object asynchronously public class Multithreader public static void main String args Runnable task1 for int i 0 i 10 i System out println i 1 System out println Printing 1 to 10 Thread thread new Thread task1 thread start System out println Done What happens at this line This program will execute concurrently and
View Full Document