Week6 - Week 6 Concurrent Programming: Threads CS 180 Sunil...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Week 6 Concurrent Programming: Threads CS 180 Sunil Prabhakar Department of Computer Science Purdue University Announcements Exam 1 October 5 6:30 pm -- 7:30 pm MTHW 210 Topics: everything upto last week (Exception handling) Project 2 Due September 30th, 9:00pm 2 Objectives This week we will get introduced to concurrent programming Creating a new thread of execution Waiting for threads to terminate Thread states and scheduling sleep() and yield() Simple synchronization among threads 3 One-track mind? Often, in real life we do multiple tasks at the same time Doing the laundry Making a pot of coffee This is more efficient. Our programs thus far have had a single track (thread) of execution at any point in time, there is only one statement being executed not always efficient -- can stall (e.g., user input) 4 Multiple concurrent tasks Consider a game program that has to repeatedly redraw the scene play the game, record scores, ask the user if they want to play again. We dont want to stop redrawing the scene while waiting for the user input. Solution: perform both tasks at the same time (concurrently) 5 Multiple cores and processors Due to the recent hardware trends, modern computers have multiple CPUs (cores or processors) If there is only a single thread of execution, only one CPU is used for our program. How do we exploit these other CPUs? Consider the initialization of a large array searching for an item in a large array Split array into pieces and initialize (search) each piece concurrently. 6 Game: sequential version 7 initializeGame(); redrawScreen(); boolean done= false ; while (!done) { done = processNextMove(); redrawScreen(); updateScores(); } terminateGame(); initializeGame(); redrawScreen(); done = processNextMove(); redrawScreen(); updateScores(); done? terminateGame(); Screen frozen while waiting for user input. Game: concurrent version 8 initializeGame(); redrawScreen(); done = processNextMove(); updateScores(); terminateGame(); Note: separate tasks (threads). No freezing done? Array: sequential version 9 final int SIZE = 1000000; double rand = new double [SIZE]; for ( int i=0;i<SIZE;i++) rand[i]= Math.random(); i=0; rand[i]=Math.random(); i++ i<SIZE? Only one thread -- may take long time; next statement Array: concurrent version 10 i=0; rand[i]=Math.random(); i++ i<mid? With concurrent execution -- may be twice as fast! i=mid; rand[i]=Math.random(); j++ j<SIZE? mid=SIZE/2; next statement Need to wait for both threads before continuing. Motivation for concurrency Need for asynchrony need to perform separate tasks e.g., game example potential for increased speed with multiple CPUs/cores e.g, matrix example Achieving these goals is not straight- forward 11 Concurrent processing How do we create a separate thread of execution?...
View Full Document

Page1 / 48

Week6 - Week 6 Concurrent Programming: Threads CS 180 Sunil...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online