Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:12:23+00:00 2026-06-03T22:12:23+00:00

I am trying to understand Clojure futures, and I’ve seen examples from the common

  • 0

I am trying to understand Clojure futures, and I’ve seen examples from the common Clojure books out there, and there are examples where futures are used for parallel computations (which seems to makes sense).

However, I am hoping someone can explain the behavior of a simple example adapted from O’Reilly’s Programming Clojure book.

(def long-calculation (future (apply + (range 1e8))))

When I try to dereference this, by doing

(time @long-calculation)

It returns the correct result (4999999950000000), but almost instantly (in 0.045 msecs) on my machine.

But when I call the actual function, like so

(time (apply + (range 1e8)))

I get the correct result as well, but the time taken is much larger (~ 5000 msecs).

When I dereference the future, my understanding is that a new thread is created on which the expression is evaluated – in which case I would expect it to take around 5000 msec as well.

How come the dereferenced future returns the correct result so quickly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:12:25+00:00Added an answer on June 3, 2026 at 10:12 pm

    The calculation in a future starts as soon as you create the future (in a separate thread). In your case, the calculation starts as soon as you execute (def long-calculation ....)

    Dereferencing will do one of two things:

    • If the future has not completed, block until it completes and then return the value (this could take an arbitrary amount of time, or even never complete if the future fails to terminate)
    • If the future has completed, return the result. This is almost instantaneous (which is why you are seeing very fast dereference returns)

    You can see the effect by comparing the following:

    ;; dereference before future completes
    (let [f (future (Thread/sleep 1000))]
      (time @f))
    => "Elapsed time: 999.46176 msecs"
    
    ;; dereference after future completes
    (let [f (future (Thread/sleep 1000))]
      (Thread/sleep 2000)
      (time @f))
    => "Elapsed time: 0.039598 msecs"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to understand that - I have never used it before. How is
I'am trying to understand the example from program_options of the boost library ( http://www.boost.org/doc/libs/1_38_0/doc/html/program_options/tutorial.html#id3761458
I'm trying to figure out corecursion in Clojure with nontrivial (i.e. not Fibonacci), but
I'm trying to understand what is the idiomatic way in Clojure to recurse through
I've spent some time, trying to understand Clojure multimethods. The main pro multimethod argument,
I am trying to understand how Clojure hierarchies work, with the derive and is-a?
This is a function taken from Pro JavaScript techniques that I'm trying to understand,
Trying to understand free-identifier=? and bound-identifier=?. Can anyone give me equivalent code examples where
I'm a Clojure newbie. I'm trying to understand why the second form doesn't work:
Trying to understand why rownames = FALSE is not passed on from Test to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.