Download presentation
Presentation is loading. Please wait.
1
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall201/ This Week: 11/14-18 1.Review 2.Thread interference 3.Synchronization
2
Readings and Exercises for Week 13 Readings: Chapter: 14.3, 14.4, 14.5; 15.3, 15.4 Exercises: 14.4, 14.9, 14.10 ©Aditya Mathur. CS 180. Fall 2011. Week 13
3
Concurrency: Review ©Aditya Mathur. CS 180. Fall 2011. Week 13
4
Thread: Typical lifecycle ©Aditya Mathur. CS 180. Fall 2011. Week 13 Define class that extends Thread. Create thread. Start thread. Wait for thread to terminate. Get results from thread. Use results from thread.
5
Thread: The constructor ©Aditya Mathur. CS 180. Fall 2011. Week 13 A class that extends Thread generally has a constructor that is used to pass parameters to a thread as follows. public Search (String [] a, String s, int start, int end, int tID, ){ // Save parameters for use when the thread executes x=a; this.s=s; this.start=start; this.end=end; this.tID=tID }
6
Use of “this” ©Aditya Mathur. CS 180. Fall 2011. Week 13 public class TestThis extends Thread { int x, y; static long z; public TestThis(int x, int y, long z){ this.x=x; // OK TestThis.y=y; // Not OK; will not compile; y must be static TestThis.z=z; // This is OK. this.z=z; // OK; static can be referenced from object. }
7
The run() method ©Aditya Mathur. CS 180. Fall 2011. Week 13 Called when the thread is started using start(). You can call the run() method directly from its thread object but then the code in the object will not execute concurrently with other thread objects.
8
Restarting a thread? ©Aditya Mathur. CS 180. Fall 2011. Week 13 No, it is illegal. Once a thread has terminated, it is illegal to restart it. Try restarting a thread to find out what happens if you do it!
9
Thread interleaving, shared data, and races ©Aditya Mathur. CS 180. Fall 2011. Week 13
10
Thread 1 S11 S12 S13 ©Aditya Mathur. CS 180. Fall 2011. Week 13 Thread 2 S21 S22 S23 S11 S12 S13 S21 S22 S23 A few possible execution interleaving: S11 S12 S21 S22 S23 S13 S21 S11 S22 S12 S23 S13 How many possible interleaving? Interleaving
11
©Aditya Mathur. CS 180. Fall 2011. Week 13 Shared data Thread 1 x=x+1; Thread 2 x=x+1; x=x+1 Get value of x; Add 1 to x; Save x in memory; int x=0;
12
©Aditya Mathur. CS 180. Fall 2011. Week 13 Interleaving and Shared data: race condition Executing Thread xInstructionEffect 10Retrieve value of xThread 1 has 0 20Retrieve value of xThread 2 has 0 10Increment value of xThread 1 has 1 20Increment value of xThread 2 has 1 11Save value of x in memoryValue of x changes to 1 21Save value of x in memoryValue of x changes to 1 Final value of x is 1 and not 2!!
13
©Aditya Mathur. CS 180. Fall 2011. Week 13 Example: Airline Seat Reservation Thread A [Mr J] Get available seat; if (seat available) Reserve seat; else Display message; Thread B [Ms M] Get available seat; if (seat available) Reserve seat; else Display message; Seat 3A Assigned to: None
14
©Aditya Mathur. CS 180. Fall 2011. Week 13 Two passengers on the same seat! Executin g Thread SeatInstructionEffect 1AGet available seatThread 1 has seat 3A 2AGet available seatThread 2 has seat 3A 1AIs seat available?Yes 2AIs seat available?Yes 1NA-JReserve 3AJ gets 3A 2NA-MReserve 3ABut then M gets 3A!! Seat 3A Assigned to: M A: available NA-X: Not available, assigned to X
15
©Aditya Mathur. CS 180. Fall 2011. Week 13 Race condition: Thread public class Count extends Thread{ Holder h; int c; public Count(Holder h, int incTimes){ this.h=h; c=incTimes; } public void run(){ for(int i=0; i<c; i++){ h.x=h.x+1; }
16
©Aditya Mathur. CS 180. Fall 2011. Week 13 Race condition: Holder class public class Holder{ int x=0; final static int incCount=10000; public static void main(String [] arg){ Holder h=new Holder(); // Create Holder object Count t1=new Count (h, incCount); Count t2=new Count (h, incCount); t1.start(); t2.start(); try{ t1.join(); t2.join(); }catch(Exception e){} System.out.println(h.x); }
17
©Aditya Mathur. CS 180. Fall 2011. Week 13 Live Example 1: Unsynchronized method Live Example 2: Synchronized method
18
Week 13: November 14-18, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. ©Aditya Mathur. CS 180. Fall 2011. Week 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.