• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Lay term or by analogy for Future interface

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If possible, can some explain the Future interface in Lay term or by analogy?
 
Master Rancher
Posts: 4806
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it as a wrapper for a future result. You've requested something, and it's not ready yet. Maybe it's being retrieved from a database, or it's being computed, or we're waiting for another system or human to respond to a message and give us some information we need. Whatever it is, the thing we want is not available yet. But we get an object that gives us the ability to check if it's available yet, and get it if and when it is available. With that object, we could choose to simple wait and do nothing else until the thing we want is ready. Or we could check periodically and see if it's available yet, doing other things in the mean time. Or we can decide we're tired of waiting, and cancel the Future - that's kind of like cancelling an order. We no longer care to receive the thing we previously requested.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now thread-wise:

Suppose there is a service (local or remote) that return a Response (any Object)

Future<Response> futureResponse = service.getResponse();

Implicitly there is a new worker thread to take care of the request freeing the calling thread to do some thing else.

Is this correct? Or correct/explain.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also sometimes called a Promise.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So who kept their promise? Jquery :-D
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H Paul wrote:Now thread-wise:

Suppose there is a service (local or remote) that return a Response (any Object)

Future<Response> futureResponse = service.getResponse();

Implicitly there is a new worker thread to take care of the request freeing the calling thread to do some thing else.

Is this correct? Or correct/explain.


Possibly. Often it's not a new thread, but one from a pre-existing thread pool. Or it might be that most of the real work is taking place on another machine entirely, elsewhere on the network. Or the thing we're waiting for might not be on a machine at all - it might be something that needs human input. There's probably a thread somewhere waiting for it - but it might even be a single thread waiting for a response to any of hundreds of requests out to other machines. So it's not necessarily one new thread per Future. The nice thing is, we don't really need to know what other threads are involved. The Future interface lets us ignore that and just find out if the result is there yet, or not.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to make an analogy, not a perfect one but it works. Let's say you go to a dry cleaner to get your clothes cleaned. You go to the counter, drop a load off. The person at the counter counts your clothes, takes your money and you clothes, and gives you a receipt. This receipt has a count of your clothes and an order number. WHen he/she bags the clothes, she puts a ticket on the bag that has the same order number and ships that bag off to the back of the house. The back of the house cleans your clothes, puts the clean clothes in a bag with the same order number. Next day, you come in with the receipt, show it at the counter. The clerk at the counter uses the receipt to check if the order is ready, if it is, s/he retreives it from the rack and hands the clothes to you.

Now, in this complete transaction, the receipt is the Future or the Promise object. What that reciept signifies is that the dry cleaner is promising you that you will get clean clothes in the future. The receipt themselves are not the clothes (Unless you are Lady Gaga, you can't wear the receipt). The receipt is something that you use to get the the clothes, after they are ready. Before the clothes are ready, you can use the reciept to check the status of your clothes. It contains enough information (mainly the order number) that the underlying system can use to find your clothes wherever they are.

Similarly, when you use a Executor, you submit a Runnable/Callable to the Executor. The Runnable/Callable represents the job that needs to be done. the submit method returns a Future object to you. The Future object is the object that you can use to get the result of the Callable method, after the Callable method has finished executing. Also, the Future method can be used to check the status of the Runnable/Callable. If the RUnnable/Callable throws Exception, you can retreive it from the Future object.
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic