Object Oriented Programming LP3 2004
Download
Report
Transcript Object Oriented Programming LP3 2004
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
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
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
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
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
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
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();}
}
}
}
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?
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();
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
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();
}
}
}
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++;
}
}
}
Multiple Counter Threads –
Blocking with join()
Public class ConcurrentCounter3{
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?
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
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; }
}
}
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; }
}
}
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?
Providing a lock for balance
public class Account{
public synchronized boolean withDraw(int amount){
private int balance;
private boolean locked = true;
while(!lock()){
try{ wait(); }
catch(InterruptedException e){ }
if(!lock()){ break; } // Got the lock?
}
public synchronized boolean lock(){
if(locked) { locked = !locked; return locked; }
else { return locked; }
}
if(balance >= amount);
int newBalance = balance - amount;
balance = newBalance;
}
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 blocked threads
}
...
lock(); // Release lock
notifyAll(); //Release blocked threads
}
return true;