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.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 15: Nov 28-Dec 2, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 10: Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
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.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 12: Nov 7-11, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Classes and Objects CSC 171 FALL 2004 LECTURE 2. LABS Labs start this week. Go to your 2 nd (Wed or Thurs) lab Expect something like the programming exercises.
Cosc 4755 Phone programming: GUI Concepts & Threads.
CSC 171 – FALL 2004 COMPUTER PROGRAMMING LECTURE 0 ADMINISTRATION.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
Shorthand operators.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Quiz 1 What is this? (explain the use of the reserved word “this” in a class method). Answer each question briefly. – What is a Constructor? –Under what.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 5: September 20-24, 2010 Aditya Mathur Department of Computer Science Purdue.
CS1101: Programming Methodology Recitation 6 – Arrays and Collections.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 25, 2004 Last update:
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 12: Nov 8-12, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.
CMSC 330: Organization of Programming Languages Threads.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette August 27, 2004 Last update:
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 10: Oct 25-29, 2010 Aditya Mathur Department of Computer Science Purdue.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science.
Multithreading Lec 23.
Multi Threading.
Lecture 9 Object Oriented Programming Using Java
Java Concurrency.
Section 5.7 Concurrency, Interference, and Synchronization.
Java Concurrency.
Multithreaded Programming in Java
CS 180 Assignment 6 Arrays.
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 4 Chapter 5 (Loops).
Objects with ArrayLists as Attributes
Lecture 19 Threads CSE /6/2019.
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Threads and concurrency / Safety
Presentation transcript:

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 This Week: 11/ Review 2.Thread interference 3.Synchronization

Readings and Exercises for Week 13 Readings: Chapter: 14.3, 14.4, 14.5; 15.3, 15.4 Exercises: 14.4, 14.9, ©Aditya Mathur. CS 180. Fall Week 13

Concurrency: Review ©Aditya Mathur. CS 180. Fall Week 13

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

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

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

The run() method ©Aditya Mathur. CS 180. Fall 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.

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

Thread interleaving, shared data, and races ©Aditya Mathur. CS 180. Fall Week 13

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

©Aditya Mathur. CS 180. Fall 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;

©Aditya Mathur. CS 180. Fall 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!!

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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; }

©Aditya Mathur. CS 180. Fall 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); }

©Aditya Mathur. CS 180. Fall Week 13 Live Example 1: Unsynchronized method Live Example 2: Synchronized method

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 Week 13