Presentation is loading. Please wait.

Presentation is loading. Please wait.

1Futures Futures contract –an agreement traded on an organized exchange to buy or sell assets (commodities or shares) at a fixed price but to be delivered.

Similar presentations


Presentation on theme: "1Futures Futures contract –an agreement traded on an organized exchange to buy or sell assets (commodities or shares) at a fixed price but to be delivered."— Presentation transcript:

1 1Futures Futures contract –an agreement traded on an organized exchange to buy or sell assets (commodities or shares) at a fixed price but to be delivered and paid for later. At a fast food shore…

2 2 Proxy Design Pattern Intent –Provide a surrogate or placeholder for another object to control access to it. An example –Image handling in a web browser When a high-resolution image is embedded in an HTML file, –Only a bounding box (fake/virtual image) is drawn first, and coordinated with surrounding text. »because most users are not patient enough to keep watching blank browser windows until both text and image are downloaded. –Whenever the image is downloaded, the bounding box is replaced with the real image. –The image is gradually drawn from a lower to higher resolutions.

3 3 > Image ImageProxy Image draw() getExtent() ImageProxy() draw() drawBBox() getExtent() fetchImage() image 1 Browser extent draw() getExtent() extent

4 4 > Image ImageProxy Image draw() getExtent() ImageProxy() draw() drawBBox() getExtent() fetchImage() image 1 Browser extent draw() getExtent() extent Obtained from a HTML file (width=“100” height=“50”) Or, the default extent used. if( image == null){ drawBBox( getExtent() ); } else { image.draw(); } if( image == null){ return extent; } else { image.getExtent(); } Image img = new ImageProxy(); img.draw(); Create a thread, and using the thread, start downloading a target image from a remote web site. Once it is done, instantiate Image and call draw() fetchImage(…);

5 5 Sample Code using Futures

6 6 > Pizza FutureRealPizza getPizza(): String setRealPizza(): void getPizza(): String realPizza 1 Casher 3 order(): Pizza p1, p2, p3

7 7 Main() :Casher new order() :Future new :Thread new :RealPizza new setRealPizza() future getPizza() pizzaData

8 8 Main() :Casher new order() :Future new :Thread new :RealPizza new setRealPizza() future getPizza() pizzaData concurrently executed realPizza Need to protect it

9 9 Another thread setRealPizza() Main thread getPizza() If(realPizza == null) --> true Reaches the end of its time slice Gain a time slice Acquire a lock realPizza = real; ready.signalAll(); Release a lock Reaches the end of its time slice (realPizza != null) Gain a time slice ready.await();

10 10 Another thread setRealPizza() Main thread getPizza() If(realPizza == null) --> true Reaches the end of its time slice Gain a time slice Acquire a lock realPizza = real; ready.signalAll(); Release a lock Reaches the end of its time slice (realPizza != null) Gain a time slice ready.await(); --> deadlock realPizza is not synchronized!

11 11 Output –Ordering pizzas at a casher counter. –An order is made. –Doing something, reading newspapers, magazines, etc., until pizzas are ready to pick up... –A real pizza is made! –Let's see if pizzas are ready to pick up... –REAL PIZZA!

12 12HW Read two papers on the proxy design pattern Modify sample code as follows: –Replace an anonymous thread class (in Casher::order()) with a Runnable class (say, the class Order) –Implement Future::isReady(): boolean True returned if a real pizza is ready; otherwise, false returned. Modify main() to use isRedy() –Implement Future::getPizza(timeout: long): String Blocks (with await()) for at most timeout (in milliseconds) if a real pizza is not ready –Timeout is the maximum time to wait. Throws an TimeoutException when a timeout occurs. –Currently, Future::getPizza() blocks until a real pizza becomes ready.

13 13 Main() :Casher new order() :Future new :Order (Runnable) new :RealPizza new setRealPizza() future getPizza() pizzaData


Download ppt "1Futures Futures contract –an agreement traded on an organized exchange to buy or sell assets (commodities or shares) at a fixed price but to be delivered."

Similar presentations


Ads by Google