Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: 11/15 1.Review 2.Thread interference 3.Synchronization 11/17 1.Review 2.Synchronization 3.Examples

2 Readings and Exercises for Week 13 Readings: Chapter: 13.3, 13.4, 13.5; 14.3, 14.4 Exercises: 13.4, 13.9, 13.10 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

3 Revised Project Schedule Project 4: Due: Deadline move to: Thursday November 18, 2010 Lab Help Sessions: Tuesday November 16. 5:30-7:30pm Thursday November 18. 5:30-7:30pm Project 5: Assigned on Wednesday November 17. Deadline: Tuesday December 7. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

4 Exam 2: Statistics Part A 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Ave: 22.3 Median: 22 Max: 30 Std: 5.37

5 Exam 2: Statistics Part B 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Ave: 56.5 Median: 59 Max: 70 STD: 11.5

6 Exam 2: Common mistakes: Use of setText() Conversion to string before using it in setText() long n; setText(n); // Incorrect setText(n.toString(); // also incorrect setText(Long.toString(n)); // correct 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

7 Exam 2: Common mistakes: Declarations Declaring a widget in main() and using it inside the actionPerformed() method. This works only if you use inner classes, which some of you have used, but not otherwise. Why? Because the widget is not visible inside actionPerformed() method. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

8 Exam 2: Common mistakes: == instead of == = is the assignment operator. == is a comparison operator. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

9 Exam 2: Common mistakes: == instead of equals() == works well for primitive types. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Use equals() when comparing objects such as strings. Given strings s1 and s2, s1 ==s2 might be false even though the strings are identical.

10 Exam 2: Common mistakes: Use of length Apply length to find length of an array. length is an attribute (or a field) of an array. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Use length() to get the length of a string.

11 Exam 2: Common mistakes: Missing return A method that has a return type other than void must return something! 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

12 Exam 2: Use of MouseListener Not a mistake but ActionListener much easier in this problem. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

13 Concurrency: Review 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

14 Thread: Typical lifecycle 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Define class that extends Thread. Create thread. Start thread. Wait for thread to terminate. Get results from thread. Use results from thread.

15 Thread: The constructor 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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 }

16 Use of “this” 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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. }

17 The run() method 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Called when the thread is started using start(). You can certainly call the run() method directly from its thread object but then the code in the object will not execute concurrently with other thread objects.

18 Restarting a thread? 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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!

19 Thread interleaving, shared data, and races 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13

20 Thread 1 S11 S12 S13 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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

21 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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;

22 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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!!

23 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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

24 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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

25 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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; }

26 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. 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); }

27 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13 Live Example 1: Unsynchronized method Live Example 2: Synchronized method

28 Week 13: November 15-19, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 11/17/2010 ©Aditya Mathur. CS 180. Fall 2010. Week 13


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue."

Similar presentations


Ads by Google