NotesWeek13.Nov 15-19

NotesWeek13.Nov 15-19 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hNp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 13: Nov 1519, 2010 Aditya Mathur 11/15 1. Review 2. Thread interference 3. SynchronizaVon Department of Computer Science Purdue University West LafayeNe, IN, USA 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/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Revised Project Schedule Project 4: Due: Deadline move to: Thursday November 18, 2010 Lab Help Session: Tuesday 5:307:30pm Project 5: To be assigned on Wednesday November 17. Deadline moved to: Monday December 6. 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Concurrency: Review 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Thread: Typical lifecycle Define class that extends Thread. Create thread. Start thread. Wait for thread to terminate. Get results from thread. Use results from thread. 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Thread: The constructor 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 } 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Use of "this" public class TestThis extends Thread { int x, y; staVc 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 staVc TestThis.z=z; // This is OK. this.z=z; // OK; staVc can be referenced from object. } } 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Thread interleaving, shared variables, and races 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Interleraving Thread 1 S11 S12 S13 Thread 2 S21 S22 S23 A few possible execuVon interleaving: S11 S12 S13 S21 S22 S23 S11 S12 S21 S22 S23 S13 S21 S11 S22 S12 S23 S13 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 How many possible interleavings? Shared data Thread 1 x=x+1; Thread 2 int x=0; x=x+1; Get value of x; x=x+1 Add 1 to x; Save x back in memory; 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Interleaving and Shared data: race condiVon Execu&ng Thread x Instruc&on Effect 1 2 1 2 1 2 0 0 0 0 0 1 Retrieve value of x Retrieve value of x Increment value of x Increment value of x Effect Thread 1 has 0 Thread 2 has 0 Thread 1 has 1 Save value of x in memory Value of x changes to 1 Save value of x in memory Value of x changes to 1 Final value of x is 1 and not 2!! Aditya Mathur. CS 180. Fall 2010. Week 13 11/15/2010 Race condiVon: 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; } } } 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Race condiVon: Holder class public class Holder{ int x=0; final staVc int incCount=10000; public staVc 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(ExcepVon e){} System.out.println(h.x); } Aditya Mathur. CS 180. Fall 2010. Week 11/15/2010 13 } Live Example 1: Unsynchronized method Live Example 2: Synchronized methods 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 Week 13: November 1519, 2010 Hope you enjoyed this week! QuesVons? Contact your recitaVon instructor. Make full use of our office hours. 11/15/2010 Aditya Mathur. CS 180. Fall 2010. Week 13 ...
View Full Document

This note was uploaded on 02/05/2012 for the course CS 180 taught by Professor Staff during the Fall '08 term at Purdue University.

Ask a homework question - tutors are online