Back

counted-thread-factory (clj)

(source)

function

(counted-thread-factory name-format daemon) (counted-thread-factory name-format daemon {:keys [init-fn], :as opts})
Create a ThreadFactory that maintains a counter for naming Threads. name-format specifies thread names - use %d to include counter daemon is a flag for whether threads are daemons or not opts is an options map: init-fn - function to run when thread is created

Examples

clojure/core.async
(ns clojure.core.async.concurrent-test
  (:require [clojure.test :refer :all]
            [clojure.core.async.impl.concurrent :as conc])
  (:import [java.util.concurrent ThreadFactory]))

(deftest test-counted-thread-factory
  (testing "Creates numbered threads"
    (let [^ThreadFactory factory (conc/counted-thread-factory "foo-%d" true)
          threads (repeatedly 3 #(.newThread factory (constantly nil)))]
      (is (= ["foo-1" "foo-2" "foo-3"] (map #(.getName ^Thread %) threads))))))