Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's pretty much how all futures are implemented. Some implementations have an executor object that determines the size of the thread pool and so forth, but the basic principle is the same.

For example, in Python it's:

    executor = ThreadPoolExecutor(max_workers=1)
    x = executor.submit(func, arg1, arg2)
    print x.result()
In Clojure it's:

    (let [x (future (func arg1 arg2))]
      (println @x))
In Java:

    ExecutorService executor = Executors.newFixedThreadPool(1);
    FutureTask<String> x =
        new FutureTask<String>(new Callable<String>() {
            public String call() {
                return SomeClass.func(arg1 arg2);
           }});
    executor.execute(x);
    System.out.println(x.get());
Basically the same idea; execute some code in a background thread and return the result when asked. If the result isn't ready, then block.

Apparently some languages use a callback to get the result, rather than blocking, but the most common approach I've seen is just to block the thread until the future is finished.



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: