cs 180 problem solving and object oriented programming fall 2010 notes for week 13: nov 15-19, 2010...

28
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

Post on 19-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

CS 180 Problem Solving and Object Oriented Programming Fall 2010

Notes for Week 13:Nov 15-19, 2010

Aditya MathurDepartment of Computer SciencePurdue UniversityWest Lafayette, IN, USA

http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/

This Week:

11/15 1. Review2. Thread interference3. Synchronization

11/17 1. Review2. Synchronization3. Examples

©Aditya Mathur. CS 180. Fall 2010. Week 13

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

Revised Project Schedule

Project 4:Due: Deadline move to:Thursday November 18, 2010Lab Help Sessions:

Tuesday November 16. 5:30-7:30pmThursday 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

Exam 2: Statistics Part A

11/17/2010

Ave: 22.3Median: 22Max: 30Std: 5.37

©Aditya Mathur. CS 180. Fall 2010. Week 13

Exam 2: Statistics Part B

11/17/2010

Ave: 56.5Median: 59Max: 70STD: 11.5

©Aditya Mathur. CS 180. Fall 2010. Week 13

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

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

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

Exam 2: Common mistakes: == instead of equals()

== works well for primitive types.

11/17/2010

Use equals() when comparing objects such as strings.

Given strings s1 and s2, s1 ==s2 might be false even though the strings are identical.

©Aditya Mathur. CS 180. Fall 2010. Week 13

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

Use length() to get the length of a string.

©Aditya Mathur. CS 180. Fall 2010. Week 13

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

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

Concurrency: Review

11/17/2010

©Aditya Mathur. CS 180. Fall 2010. Week 13

Thread: Typical lifecycle

11/17/2010

Define class that extends Thread.

Create thread.

Start thread.

Wait for thread to terminate.

Get results from thread.

Use results from thread.

©Aditya Mathur. CS 180. Fall 2010. Week 13

Thread: The constructor

11/17/2010

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

}

©Aditya Mathur. CS 180. Fall 2010. Week 13

Use of “this”

11/17/2010

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.

}

}

©Aditya Mathur. CS 180. Fall 2010. Week 13

The run() method

11/17/2010

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.

©Aditya Mathur. CS 180. Fall 2010. Week 13

Restarting a thread?

11/17/2010

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!

©Aditya Mathur. CS 180. Fall 2010. Week 13

Thread interleaving, shared data, and races

11/17/2010

©Aditya Mathur. CS 180. Fall 2010. Week 13

Thread 1S11S12S13

11/17/2010

Thread 2S21S22S23

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

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

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;

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

Interleaving and Shared data: race condition

Executing Thread

x Instruction Effect

1 0 Retrieve value of x Thread 1 has 0 2 0 Retrieve value of x Thread 2 has 01 0 Increment value of x Thread 1 has 12 0 Increment value of x Thread 2 has 11 1 Save value of x in memory Value of x changes to 12 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/17/2010

Example: Airline Seat Reservation

Thread A [Mr J]

Get available seat;

if (seat available)Reserve seat;

elseDisplay message;

Thread B [Ms M]

Get available seat;

if (seat available)Reserve seat;

elseDisplay message;

Seat 3A

Assigned to: None

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

Two passengers on the same seat!

Executing Thread

Seat Instruction Effect

1 A Get available seat Thread 1 has seat 3A 2 A Get available seat Thread 2 has seat 3A1 A Is seat available? Yes2 A Is seat available? Yes1 NA-J Reserve 3A J gets 3A2 NA-M Reserve 3A But then M gets 3A!!

Seat 3A

Assigned to: M

A: availableNA-X: Not available, assigned to X

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

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; } }}

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

Race condition: Holder classpublic 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); }}

©Aditya Mathur. CS 180. Fall 2010. Week 13

11/17/2010

Live Example 1: Unsynchronized method

Live Example 2: Synchronized method

©Aditya Mathur. CS 180. Fall 2010. Week 13

Week 13: November 15-19, 2010Hope you enjoyed this week!

Questions?

Contact your recitation instructor. Make full use of our office hours.

11/17/2010