1 / 18

Object Oriented Programming

Object Oriented Programming. Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads www.hh.se/staff/jebe/oop2006/index.html. Last lecture – Short recapitulation. A case study – The Algorithm Animator

ipo
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads www.hh.se/staff/jebe/oop2006/index.html

  2. Last lecture – Short recapitulation • A case study – The Algorithm Animator • Demonstrating practical appliance of • The Template pattern • A generic animator template for sort algorithms • The Strategy pattern • Decoupling the sort algorithms • The Factory Pattern • Decoupling creation of specific sort algorithms • The Adapter pattern • Narrowing and conversion of class interfaces

  3. Laboratorial Exercise – Part II • A Graphical User Interface for the SearchEngine • Making queries • Input in terms keywords • Ex. A text field • Search button • Searching by logical AND using a string of keywords • Menu bar • Fetch doc • Fetch and show a doc with a specific title • Add doc • Add a document to the engine • Quit

  4. GUI Forms in Netbeans • Netbeans visual GUI development • Both Swing and AWT supported • How to couple Engine with the GUI? • Create a new package for your GUI in the project folder • Import the Engine package • Demonstration of Netbeans GUI forms

  5. Multi-threaded programming • Most programming languages are sequential single-thread programs • Multi-threaded programs • Multiple, simultaneous threads can run either • Sequentially on single processor • Concurrently on multiple processors • Scheduled by the operating system

  6. Multi-threading in Java • Threads are objects • Instances of the java.lang.Thread class • Created by extending Thread • Implementing the Runnable interface • All threads executed by the JVM share memory space • Threads can communicate through / access shared variables and objects

  7. Example – A Concurrent Counter public class Counter extends Thread{ static int id = 0; int value, bound, delay, myid; public Counter(int start, int stop, int d){ value = start; bound = stop; delay = d; myid = id; ++id; } public void run(){ while(value < bound){ System.out.println("Thread nbr " + myid +": " + value); value++; try{ sleep(delay); } catch(InterruptedException ie){ ie.printStackTrace();} } } }

  8. Creating Multiple Counter Threads Public class Concurrent Counters{ public static void main(String args[]){ for(int i = 0; i<10; i++){ new Counter(0,20,50).start(); } } } Question: How many Threads are running?

  9. Controlling Threads • Threads are controlled by the JVM • Scheduled by priority (an integer value) • Threads with equal priority will be executed in arbitrary order • No fairness guarantee • Thread scheduling can also be affected by the programmer • Setting thread priorities • Can be done dynamically (at runtime) • By blocking or yielding • sleep(); yield; ... join();

  10. Blocked and Runnable states • Blocked mode • In sleep or waiting for a target to finish • sleep(milliseconds); • Runnable -> Blocked • join(); • Runnable -> Blocked • Runnable mode • Ready to scheduled and run • yield(); • Runnable -> Runnable • interrupt(); • Blocked -> Runnable or Runnable with IR-flag

  11. Example 2 – Concurrent Counter public class Counter2 extends Thread{ static int id = 0; int value, bound, delay, myid; public Counter2(int start, int stop, int d){ value = start; bound = stop; delay = d; myid = id; ++id; } public void run(){ while(value < bound){ System.out.println("Thread nbr " + myid +": " + value); value++; /** Go to blocked state **/ yield(); } } }

  12. Example 3 – Concurrent Counter public class Counter extends Thread{ static int id = 0; int value, bound, delay, myid; public Counter(int start, int stop, int d){ value = start; bound = stop; delay = d; myid = id; ++id; } public void run(){ while(value < bound){ System.out.println("Thread nbr " + myid +": " + value); value++; } } }

  13. Multiple Counter Threads – Blocking with join() Public class Concurrent Counter3{ for(int i = 0; i<10; i++){ Counter3 cnt = new Counter3(0,20,50); cnt.start(); try{ cnt.join(); } catch(InterruptedException ie){} } } Question: In what order will threads be executed now?

  14. Critical regions • Objects that are manipulated by multiple threads constitute a critical region • Calls to methods or/and access of shared variables • race hazard or race condition between threads • Java provide synchronized to warrant atomic access to critical regions

  15. Example of critical region: Bank account public class Account{ private int balance; public Account(){ balance = 0;} public boolean insert(int amount){ ... } public boolean withDraw(int amount){ if(balance >= amount){ newBalance = balance - amount; balance = newBalance; return true; } else { return false; } } }

  16. A synchronized withDraw()in Bank account public class Account{ private int balance; public Account(){ balance = 0;} public boolean insert(int amount){ ... } public synchronized boolean withDraw(int amount){ if(balance >= amount){ newBalance = balance - amount; balance = newBalance; return true; } else { return false; } } }

  17. Bank account: insert() and withDraw() public class Account{ private int balance; public Account(){ balance = 0;} public synchronized boolean insert(int amount){ int newBalance = balance + amount; balance = newBalance; } public synchronized boolean withDraw(int amount){ if(){ balance >= amount; int newBalance = balance - amount; balance = newBalance; return true; } else { return false; } } } Question: Consider both synchronized insert and withDraw, are we safe with this?

  18. public class Account{ private int balance; private boolean locked = true; public synchronized boolean lock(){ if(locked) { locked = !locked; return locked; } else { return locked; } } public synchronized void insert(int amount){ while(!lock()){ try{ wait(); } catch(InterruptedException e){ } if(!lock()){ break; } // Got the lock? } int newBalance = balance + amount; balance = newBalance; lock(); // Release lock notifyAll(); //Release locked threads } ... public synchronized boolean withDraw(int amount){ while(!lock()){ try{ wait(); } catch(InterruptedException e){ } if(!lock()){ break; } // Got the lock? } if(balance >= amount); int newBalance = balance - amount; balance = newBalance; } lock(); // Release lock notifyAll(); //Release blocked threads return true; } Providing a lock for balance

More Related