aspose file tools
The moose likes Beginning Java and the fly likes ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(); Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();" Watch "ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();" New topic
Author

ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Can you clarify why an ArrayList that according to my interpretation, contains Future (rather than FutureTask - which is an implementation of interface Future) threads (objects) instantiated with Integers (line 59) were used in the following code snippet?


source Core Java Vol.1 (8th Ed) Chapter 14, Listing 14.11





Jon
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Jon Camilleri wrote:Can you clarify why an ArrayList that according to my interpretation, contains Future (rather than FutureTask - which is an implementation of interface Future) threads (objects) instantiated with Integers (line 59) were used in the following code snippet?

If your question is specifically about why an ArrayList<Future<Integer>> rather than an ArrayList<FutureTask<Integer>> is used: that's because in general it is better to program against an interface, not an implementation. In fact, that advice has been followed only halfway in the code, it would have been more consistent if the variable was declared as a List instead of an ArrayList:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 276


to elaborate the idea on "program against interface and not on implementation" you can also refer Head first design pattern book. main purpose of these kind of code is they are more maintainable and more flexible to adapt any change in future.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Javin Paul wrote:
to elaborate the idea on "program against interface and not on implementation" you can also refer Head first design pattern book. main purpose of these kind of code is they are more maintainable and more flexible to adapt any change in future.


Sure, is it this book?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
 
Similar Threads
A Timeout scenario
Overwriting Objects in ArrayList