Synchronous concurrency: Difference between revisions

Content deleted Content added
move the "Capability:" part into the template.
Line 161: Line 161:
The ''state'' argument is the agent's state at the start of the call, and the last expression becomes the agent's new state.
The ''state'' argument is the agent's state at the start of the call, and the last expression becomes the agent's new state.


<lang clojure>(import '(java.io FileReader BufferedReader) ;used in the reader
<lang clojure>(use '[clojure.java.io :as io])


(def writer (agent 0))
(def writer (agent 0))
Line 171: Line 171:
The reader executes on the main thread. It passes each line to the writer -- the ''send'' call returns immediately, waits until the writer has finished writing all the lines, gets the line count & prints it, and terminates the writer.
The reader executes on the main thread. It passes each line to the writer -- the ''send'' call returns immediately, waits until the writer has finished writing all the lines, gets the line count & prints it, and terminates the writer.


<lang clojure>(doseq [line (-> "input.txt" FileReader. BufferedReader. line-seq)]
<lang clojure>(with-open [r (io/reader "input.txt")]
(send writer write-line line))
(doseq [line (line-seq r)]
(send writer write-line line)))
(await writer)
(await writer)
(println "lines written:" @writer)
(println "lines written:" @writer)