-3

Please can someone advise how to use Callable and Future in this scenario

I want to be able to submit mulitple bids for items from different users.

I am starting a new thread and when the winning bid value comes back I wanted to return the value. How do I spawn a new thread and return a value as soon as I have one. Will having Future.get() as the next line wait for the value to return or can it return empty value?

public AuctionBid getWinningBid(String itemCode){
    Callable<AuctionBid> winningBidCallable = () -> store.getWinningBid(itemCode);
    Future<AuctionBid> future = executorService.submit(winningBidCallable);
     return Future.get(); // can this return empty object
}
serah
  • 2,057
  • 7
  • 36
  • 56
  • 4
    Have you checked out the Javadoc documentation for the `Future.get()` method? It says "Waits if necessary for the computation to complete, and then retrieves its result.". Do you have a question about that? – Erwin Bolwidt Jul 21 '16 at 12:42
  • "can this return empty object" What do you mean by the "empty object"? It can return anything you can store in an `AuctionBid` reference, including `null`, some cached instance, or a newly-created instance. – Andy Turner Jul 21 '16 at 12:45
  • You'll want to use a callback mechanism of some sort. – Hovercraft Full Of Eels Jul 21 '16 at 12:45
  • @HovercraftFullOfEels doesn't the `Future` interface remove the need for some sort of callback? I mean, OP could just return the `Future`. – Andy Turner Jul 21 '16 at 12:46
  • 1
    Since the OP mentions that he wants to submit multiple bids, I assume that he wants to wait until all of a list/set of futures have a result and then return the best result. If that's the case then OP should look at this question: http://stackoverflow.com/questions/19348248/waiting-on-a-list-of-future – Erwin Bolwidt Jul 21 '16 at 12:48
  • I belive I am not making use of Future correctly here. I want to be able to return winning bids for any itemcode given. The winning bid for each item is stored within the store for each item so can be retrieved immediately. I want to be able to take multiple requests from users and return winning bids. What approach can I take here if the above code is incorrect? – serah Jul 21 '16 at 13:59

1 Answers1

-1

Future.get() is a blocking call, So It will wait until Future has any value(provided there is no exception). So in your case, Future.get() can fulfill your requirement.

pbajpai
  • 1,303
  • 1
  • 9
  • 24
  • His code fulfills no useful requirement since he creates a thread and immediately blocks the current thread with the call to get. – Hovercraft Full Of Eels Jul 21 '16 at 12:46
  • @Hovercraft Full Of Eels - This is where I am not clear how to proceed further. My store has bids for items from different users. Given a item code I want to return the winning bid for the item. At the same time I want it to be multithreaded. Any suggesstions, how I can approach this? As you have rightly pointed out usage of Future here does not help spawn multiple threads that can return values. – serah Jul 21 '16 at 13:52
  • @Kar: Erwin Bolwidt already gave you a link to what might help you -- a CompletionService. – Hovercraft Full Of Eels Jul 21 '16 at 13:56