Synchronous concurrency: Difference between revisions

Content added Content deleted
(changed lang tag for Clojure)
Line 160: Line 160:
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 lisp>(import '(java.io FileReader BufferedReader) ;used in the reader
<lang clojure>(import '(java.io FileReader BufferedReader) ;used in the reader


(def writer (agent 0))
(def writer (agent 0))
Line 170: Line 170:
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 lisp>(doseq [line (-> "input.txt" FileReader. BufferedReader. line-seq)]
<lang clojure>(doseq [line (-> "input.txt" FileReader. BufferedReader. line-seq)]
(send writer write-line line))
(send writer write-line line))
(await writer)
(await writer)