• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic