<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged future</title>
<link>https://ask.clojure.org/index.php/tag/future</link>
<description></description>
<item>
<title>How to force a timeout?</title>
<link>https://ask.clojure.org/index.php/13545/how-to-force-a-timeout</link>
<description>&lt;pre&gt;&lt;code&gt;(defn tid []                                                        
  [(.getName (Thread/currentThread))                                                        
   (.getId (Thread/currentThread))])

   (defn atomize                                                   
  [f data]                                                                                  
  (let [atomix (atom [])                                                                    
        pool (Executors/newFixedThreadPool 12)                                              
        tasks (map                                                                          
               (fn [datum]                                                                  
                 (fn []                                                                     
                   (swap! atomix conj [(f datum) (tid)])))                                  
               data)]                                                                       
    (doseq [future (.invokeAll pool tasks)]                                                 
      (.get future  10 TimeUnit/MILLISECONDS ))                                             
    (.shutdown pool)                                                                        
    @atomix))




I can't figure out where to place a `sleep` to force a timeout. 
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13545/how-to-force-a-timeout</guid>
<pubDate>Tue, 12 Dec 2023 13:56:49 +0000</pubDate>
</item>
<item>
<title>Future doesn't stop running</title>
<link>https://ask.clojure.org/index.php/12637/future-doesnt-stop-running</link>
<description>&lt;pre&gt;&lt;code&gt;(defn get-result-from-first-engine
&quot;Performs search on Powells and Cultura and returns the results
which comes first&quot;
[query]
(let [result (promise)]
    (doseq [engine [&quot;powells&quot; &quot;cultura&quot;]]
        (future (if-let [engine-result (search query engine)]
            (deliver result engine-result)
        ))
    )
    (println &quot;First result: &quot; (deref result 2000 &quot;timed out&quot;))
))

(defn -main
    [&amp;amp; args]

    (time (do
        (println (search &quot;clojure&quot; &quot;powells&quot;))
        (println (search &quot;clojure&quot; &quot;cultura&quot;))
    ))

    (time (get-result-from-first-engine &quot;clojure&quot;))
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code is part of my solution to the first exercise from chapter 9 of &quot;Clojure for the Brave and True&quot;. I want to perform the two requests in parallel and get the response of the one who process first.&lt;/p&gt;
&lt;p&gt;Two weird things are happening:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The time from the parallel is WAY lower than the sequential one, written in main. Result example below. The time difference is so discrepant, that i guess if clojure caches it someway. I didn't use memoize or anything like that.&lt;/li&gt;
&lt;li&gt;The main doesn't stop running. It holds like if it was waiting for an input or processing something. My theory is that the future doesn't stop running, but i don't know why&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Time difference between parallel and sequential:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;https://www.powells.com/book/clojure-cookbook-9781449366179
https://www3.livrariacultura.com.br/clojure-94991222/p
&quot;Elapsed time: 2185.2716 msecs&quot;
First result:  https://www3.livrariacultura.com.br/clojure-94991222/p
&quot;Elapsed time: 63.5558 msecs&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thanks for the help in advance&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12637/future-doesnt-stop-running</guid>
<pubDate>Fri, 03 Feb 2023 15:32:19 +0000</pubDate>
</item>
<item>
<title>Could realized? support Java futures?</title>
<link>https://ask.clojure.org/index.php/11913/could-realized-support-java-futures</link>
<description>&lt;p&gt;I'm running into a situation where I would need to mix CompletableFutures in with Clojure futures/delays/etc. Unfortunately, IPending is an interface, which can't be extended to Java core classes.&lt;/p&gt;
&lt;p&gt;For IDeref and IBlockingDeref, it's not a problem working with Java Futures, because the &lt;code&gt;deref&lt;/code&gt; fn supports them.&lt;/p&gt;
&lt;p&gt;Couldn't the same be done with the &lt;code&gt;realized?&lt;/code&gt; fn? Currently, it only works with IPending. It would be ideal if it matched the &lt;code&gt;deref&lt;/code&gt; behavior and called &lt;code&gt;.isDone()&lt;/code&gt; when given a java.util.concurrent.Future.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11913/could-realized-support-java-futures</guid>
<pubDate>Sat, 28 May 2022 07:16:15 +0000</pubDate>
</item>
</channel>
</rss>