Spe*_*pel 1 concurrency multithreading clojure terminate pmap
我正在用Clojure编写一个程序,该程序从文本文件10000.txt(具有10k,无符号整数)接受输入。然后,我将该列表传递到我的归类排序函数中(单个线程,2、4、8、16、32线程)。
当我运行该程序时,通过键入“ clojure test.clj”,它会输出每个函数的经过时间,但该程序不会终止。
它像正在等待输入或即将输出其他内容一样挂在那里。然后大约1分钟后,程序最终终止。幕后一定有事发生。输入后终止程序有什么想法/我需要做什么?
程序的输出(终止之前),这是挂起大约1分钟的地方

(use 'clojure.java.io)
(require '[clojure.string :as str])
;Read file and store into numbers, as a string
(def numbers (slurp "10000.txt"))
;Parse the string 'numbers', ignore the spaces
;and save the result into x1 (a lazy-seq of integers)
(def x1 (map #(Integer/parseInt %) (str/split numbers #"\s+")))
;Function that performs the operation of merge sort algorithm
(defn merge-lists [left right]
(loop [head [] L left R right]
(if (empty? L) (concat head R)
(if (empty? R) (concat head L)
(if (> (first L) (first R))
(recur (conj head (first R)) L (rest R))
(recur (conj head (first L)) (rest L) R))))))
;The other merge-sort functions use pmap to run merge sort in parallel
;Using 1,2,4,8,16,32 threads
(defn naive-merge-sort [list]
(if (< (count list) 2) list
(apply merge-lists
(map naive-merge-sort
(split-at (/ (count list) 2) list)))))
(defn parallel-merge-sort-2 [list]
(if (< (count list) 2) list
(apply merge-lists
(pmap naive-merge-sort
(split-at (/ (count list) 2) list)))))
(defn parallel-merge-sort-4 [list]
(if (< (count list) 2) list
(apply merge-lists
(pmap parallel-merge-sort-2
(split-at (/ (count list) 2) list)))))
(defn parallel-merge-sort-8 [list]
(if (< (count list) 2) list
(apply merge-lists
(pmap parallel-merge-sort-4
(split-at (/ (count list) 2) list)))))
(defn parallel-merge-sort-16 [list]
(if (< (count list) 2) list
(apply merge-lists
(pmap parallel-merge-sort-8
(split-at (/ (count list) 2) list)))))
(defn parallel-merge-sort-32 [list]
(if (< (count list) 2) list
(apply merge-lists
(pmap parallel-merge-sort-16
(split-at (/ (count list) 2) list)))))
;Run each of the merge-sort functions and output their time
(time (naive-merge-sort x1))
(time (parallel-merge-sort-2 x1))
(time (parallel-merge-sort-4 x1))
(time (parallel-merge-sort-8 x1))
(time (parallel-merge-sort-16 x1))
(time (parallel-merge-sort-32 x1))
Run Code Online (Sandbox Code Playgroud)
这是我的10000.txt文件:https ://pastebin.com/5vKXUk1u
我的预期结果是程序在最终时间打印后终止,而不用花1分钟时间终止。
谢谢大家的时间和帮助!
您需要shutdown-agents在最后调用以停止Clojure的线程池。
另请参见clojure.org上的代理和异步操作:
请注意,使用代理会启动非守护程序后台线程池,这将防止JVM关闭。使用shutdown-agents终止这些线程并允许关机。