Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Java <Future> without a thread

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can we use Java <Future> without using any thread?

Just to executed several methods. Can we use future for it...


Thanks
 
Saloon Keeper
Posts: 11881
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be the point?
 
Johnny Joseph
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A program wants to do computation of results....But he thinks future does that...Manages it...But he asked whether can we use the Future without threads..basically the same class
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can. You could write a method which returns a Future<Result> instead of just a Result, then call that method, then extract the Result from the Future<Result>. Future is an interface, after all, and you can implement it however you like. Just don't expect magic to happen just because you use Future.
 
Rancher
Posts: 3503
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a bit like getting a pair of skis to go skiing in winter at a ski resort.  And then in summer, asking if you can use the skis as you hike through a rocky desert.  Yes, it's possible to bring the skis along, and maybe you can use them along the way as... walking sticks?  Part of an improvised stretcher?  Probably nothing useful, really.  And it would completely miss the point of what the skis are actually for.

It sounds like maybe you have some code that calculates something useful, and that code is currently inside a class that implements Future.  And you want to use that useful code somewhere else, without using threads.  That's fine.  The key is, the useful code doesn't have to be inside that class that implements Future.  It can instead be inside another class, and when you need a Future you can give the future a reference to and instance of this other class.  So you can put the useful code together with a Future, if that's what you need.  Or you can use it separately, if that's what you need.  But don't try to use a Future where there's no use for a Future.
 
Sheriff
Posts: 7051
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johnny Joseph wrote:...But he thinks future does that...Manages it...But he asked whether can we use the Future without threads..


Who is "he"?  If it's a manager, politely ask him what he wants to accomplish.  If he presses you, tell him there's no good reason to use Future without threads.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johnny Joseph wrote:A program wants to do computation of results....But he thinks future does that...Manages it...



No, that's not quite the right way to look at it. The computation is going on in a different thread, and the Future object is just a tool which allows this thread to get the result when it's available. There's a few other methods, like you can see whether the result is available yet and you can try to cancel the computation. That's pretty much it. You just hand over the computation to a different thread, and you can ask "Are you done yet?" and you can get the result when it's done. None of those things work unless there's another thread doing the computation.
 
Johnny Joseph
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, thanks for your clear explanation. I agree future is for threads...

Do we have a similar kind of interface in Java which we can use it for computation of results???

Why we always need to create an interface for the same..Why don't java provide one so that it would be helpful to standardize.
 
Stephan van Hulst
Saloon Keeper
Posts: 11881
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want an interface that represents a task to perform, use Runnable. If the task should yield a result, use Callable.
 
Johnny Joseph
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Stephan.
 
Johnny Joseph
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share an example of Runnable with Callable, which executes multiple and combine the results..
 
Stephan van Hulst
Saloon Keeper
Posts: 11881
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, because your use case is very vague. You need to give us a better explanation of why you need a common interface for your computations if you don't intend to run them in parallel.

What are you trying to do EXACTLY?
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic