std.concurrent

->timeunit

added in 3.0

(->timeunit obj)

returns the timeunit

(q/->timeunit :ms) => java.util.concurrent.TimeUnit/MILLISECONDS

all-stacktraces

added in 3.0

(all-stacktraces)

returns all available stacktraces

aq:executor

added in 3.0

(aq:executor {:keys [target], :as opts})

creates a executor that takes in an atom queue

aq:new

added in 3.0

(aq:new)

creates an atom with a vec as queue

aq:process

added in 3.0

(aq:process f aq max-batch)

processes

(def +state+ (atom []))

(aq:process (fn elems(swap! +state+ conj elems)) (atom 1 2 3 4 5) 3)

@+state+ => [1 2 3 4 5]

aq:submit

added in 3.0

(aq:submit executor queue {:keys [target handler max-batch interval], :as opts})

submission function for one or multiple entries to aq

bulk

added in 3.0

(bulk client thunk)(bulk client thunk opts)

allows query inputs to be combined

bulk:inputs

added in 3.0

(bulk:inputs client thunk)

capture all inputs on the client

bulk:map

added in 3.0

(bulk:map client f inputs)(bulk:map client f inputs opts)

map function across a client

bulk:transact

added in 3.0

(bulk:transact client thunk)(bulk:transact client thunk opts)

creates a transaction within a bulk context

bus

added in 3.0

(bus)(bus m)

creates and starts a bus

bus:all-ids

added in 3.0

(bus:all-ids {:keys [state], :as bus})

returns all registered ids

(bus:with-temp bus (bus:all-ids bus)) => (contains #{string?})

bus:all-threads

added in 3.0

(bus:all-threads {:keys [state], :as bus})

returns all registered threads

(bus:with-temp bus (= (first (vals (bus:all-threads bus))) (Thread/currentThread))) => true

bus:close

added in 3.0

(bus:close bus id)

bus:closes all bus:opened loops

(bus:with-temp bus (let [{:keys stopped id} @(bus:open bus (fn m (update m :value inc)))] (Thread/sleep 10) (bus:close bus id) @stopped (bus:get-count bus))) => (contains-in {:exit :normal, :id string? :unprocessed empty? :start number? :end number?} 1)

bus:close-all

added in 3.0

(bus:close-all bus)

stops all thread loops

bus:create

added in 3.0

(bus:create)(bus:create m)

creates a bus

bus:deregister

added in 3.0

(bus:deregister bus)(bus:deregister {:keys [state], :as bus} id)

deregisters from the bus

bus:get-count

added in 3.0

(bus:get-count {:keys [state], :as bus})

returns the number of threads registered

bus:get-id

added in 3.0

(bus:get-id bus)(bus:get-id {:keys [state]} thread)

gets registered id given thread

(bus:with-temp bus (bus:get-id bus)) => string?

bus:get-queue

added in 3.0

(bus:get-queue bus)(bus:get-queue {:keys [state], :as bus} obj)

gets the message queue associated with the thread

(bus:with-temp bus (vec (bus:get-queue bus))) => []

bus:get-thread

added in 3.0

(bus:get-thread {:keys [state]} id)

gets thread given an id

(bus:with-temp bus (->> (bus:get-id bus) (bus:get-thread bus))) => (cc/thread:current)

bus:has-id?

added in 3.0

(bus:has-id? {:keys [state]} id)

checks that the bus has a given id

bus:kill

added in 3.0

(bus:kill bus id)

bus:closes all bus:opened loops

bus:kill-all

added in 3.0

(bus:kill-all bus)

stops all thread loops

bus:open

added in 3.0

(bus:open bus handler)(bus:open bus handler {:keys [id], :as opts, :or {id (std.lib.foundation/sid)}})

bus:opens a new handler loop given function

(bus:with-temp bus (let [{:keys id} @(bus:open bus (fn m (update m :value inc)))] (Thread/sleep 100) @(bus:send bus id {:value 1}))) => (contains {:value 2, :id string?})

bus:register

added in 3.0

(bus:register bus)(bus:register bus id)(bus:register {:keys [state], :as bus} id thread)

registers a thread to the bus

bus:reset-counters

added in 4.0

(bus:reset-counters bus)

resets the counters for a bus

bus:send

added in 3.0

(bus:send {:keys [results counters], :as bus} id msg)

sends a message to the given thread

bus:send-all

added in 3.0

(bus:send-all bus msg)

bus:sends message to all thread queues

bus:wait

added in 3.0

(bus:wait bus)(bus:wait bus {:keys [timeout timeunit], :or {timeunit :ms}})

bus:waits on the message queue for message

(bus:with-temp bus (bus:send bus (bus:get-id bus) {:op :hello :message “world”}) (bus:wait bus)) => (contains {:op :hello, :message “world”, :id string?})

(bus:with-temp bus (bus:wait bus {:timeout 100}))

bus:with-temp

macro

added in 3.0

(bus:with-temp var & body)

checks if object is instance of Bus

bus?

added in 3.0

(bus? obj)

checks if object is instance of Bus

deque

added in 3.0

(deque)(deque & elements)

constructs a blocking deque

(q/deque 1 2 3) => java.util.concurrent.LinkedBlockingDeque

deque?

added in 3.0

(deque? obj)

checks if object is a BlockingDeque

drain

added in 3.0

(drain queue)(drain queue max)(drain queue max target)

drains elements to another vector

((juxt #(q/drain % 2) #(into [] %)) (q/queue 1 2 3 4)) => [1 2 3 4]

exec:at-capacity?

added in 3.0

(exec:at-capacity? executor)

checks if executor is at capacity

exec:await-termination

added in 3.0

(exec:await-termination service)(exec:await-termination service ms)

await termination for executor service

exec:current-active

added in 3.0

(exec:current-active service)

returns number of active threads in pool

exec:current-completed

added in 3.0

(exec:current-completed service)

returns number of completed tasks

exec:current-size

added in 3.0

(exec:current-size service)

returns number of threads in pool

exec:current-submitted

added in 3.0

(exec:current-submitted service)

returns number of submitted tasks

exec:get-queue

added in 3.0

(exec:get-queue service)

gets the queue from the executor

(-> (executor:pool 10 10 1000 (q/queue)) (exec:get-queue))

exec:increase-capacity

added in 3.0

(exec:increase-capacity executor)(exec:increase-capacity executor n)

increases the capacity of the executor

exec:keep-alive

added in 3.0

(exec:keep-alive service)(exec:keep-alive service length)

gets and sets the keep alive time

exec:pool-max

added in 3.0

(exec:pool-max service)(exec:pool-max service size)

gets and sets the core pool max

exec:pool-size

added in 3.0

(exec:pool-size service)(exec:pool-size service size)

gets and sets the core pool size

exec:queue

added in 3.0

(exec:queue)(exec:queue arg)

contructs a raw queue in different ways

(exec:queue)

(exec:queue 1)

(exec:queue {:size 1})

(exec:queue {})

(exec:queue (q/queue))

exec:rejected-handler

added in 3.0

(exec:rejected-handler service)(exec:rejected-handler service f)

sets the rejected task handler

exec:shutdown

added in 3.0

(exec:shutdown service)

shuts down executor

exec:shutdown-now

added in 3.0

(exec:shutdown-now service)

shuts down executor immediately

exec:shutdown?

added in 3.0

(exec:shutdown? service)

checks if executor is shutdown

exec:terminated?

added in 3.0

(exec:terminated? service)

checks if executor is shutdown and all threads have finished

exec:terminating?

added in 3.0

(exec:terminating? service)

check that executor is terminating

executor

multimethod

added in 3.0

creates an executor

(executor {:type :pool :size 3 :max 3 :keep-alive 1000})

executor:cached

added in 3.0

(executor:cached)

creates a cached executor

(executor:cached)

executor:health

added in 3.0

(executor:health executor)

returns health of the executor

executor:info

added in 3.0

(executor:info executor)(executor:info executor k)

returns executor service info

executor:kill

executor:pool

added in 3.0

(executor:pool size max keep-alive)(executor:pool size max keep-alive size-or-queue)

constructs a pool executor

(executor:pool 10 10 1000 {:size 10})

executor:props

added in 3.0

(executor:props executor)

returns props for getters and setters

executor:scheduled

added in 3.0

(executor:scheduled size)

constructs a scheduled executor

(executor:scheduled 10)

executor:share

added in 3.0

(executor:share id executor)

registers a shared executor

executor:shared

added in 3.0

(executor:shared)

lists all shared executors

(-> (executor:shared) keys sort) => :async :default :pooled

executor:single

added in 3.0

(executor:single)(executor:single size-or-queue)

constructs a single executor

;; any sized pool (executor:single)

;; fixed pool (executor:single {:size 10})

executor:start

executor:started?

executor:stop

executor:stopped?

executor:submit

executor:type

added in 3.0

(executor:type executor)

returns executor service type

executor:unshare

added in 3.0

(executor:unshare id)

deregisters a shared executor

get-bus

added in 3.0

(get-bus)

gets the common stream bus

hub-state

added in 4.0

(hub-state elems)

creates a hub state

hub:add-entries

added in 3.0

(hub:add-entries hub entries)

adds entries to the hub

(hub:add-entries (hub:new) 1 2 3 4 5) => (contains f/future? 0 5)

hub:executor

added in 3.0

(hub:executor object {:keys [handler max-batch interval], :as opts})

creates a hub based executor

(def -exe- (hub:executor nil {:handler (fn & args args) :interval 50 :max-batch 1000}))

(do (def -res- ((:submit -exe-) 1 2 3 4 5 6)) @(first -res-)) => ’(nil (1 2 3 4 5 6))

(hub:wait (:queue -exe-)) => nil

hub:new

added in 3.0

(hub:new)(hub:new elems)

creates a trackable atom queue

hub:process

added in 3.0

(hub:process f hub max-batch)

like aq:process but with a hub

(def +state+ (atom []))

(hub:process (fn elems(swap! +state+ conj elems)) (hub:new 1 2 3 4 5) 3)

@+state+ => [1 2 3 4 5]

hub:submit

added in 3.0

(hub:submit object executor hub {:keys [handler max-batch interval]})

submission function for the hub

hub:wait

added in 4.0

(hub:wait hub & [timeout])

waits for the hub executor to be ready

peek

added in 3.0

(peek queue)

takes element at the front of the queue

(q/peek (q/queue))

peek-first

added in 3.0

(peek-first queue)

peeks from the front of the queue

peek-last

added in 3.0

(peek-last queue)

peeks from the back of the queue

pool

added in 3.0

(pool {:keys [size max keep-alive poll resource], :as m})

creates and starts the pool

(def -p- (pool {:size 2 :max 10 :keep-alive 10000 :poll 20000 :resource {:create (fn ) :initial 0.3 :thread-local true}}))

pool:acquire

added in 3.0

(pool:acquire {:keys [state resource], :as pool})

acquires a resource from the pool

(pool:acquire |pool|) => (contains string? ’)

pool:cleanup

added in 3.0

(pool:cleanup {:keys [state], :as pool})

runs cleanup on the pool

pool:create

added in 3.0

(pool:create m)

creates an initial pool

(pool:create {:size 5 :max 8 :keep-alive 10000 :poll 20000 :resource {:create (fn [] ’) :initial 0.3 :thread-local true}})

pool:dispose

added in 3.0

(pool:dispose {:keys [state resource], :as pool} id)

disposes an idle object

(pool:dispose |pool| (first (keys (pool:resources:idle |pool|))))

pool:dispose-over

added in 3.0

(pool:dispose-over {:keys [state resource], :as pool} id)

disposes if idle and busy are over size limit

pool:dispose:mark

added in 3.0

(pool:dispose:mark)

marks the current resource for dispose

pool:dispose:unmark

added in 3.0

(pool:dispose:unmark)

unmarks the current resource for dispose

pool:release

added in 3.0

(pool:release pool id)(pool:release {:keys [state], :as pool} id dispose?)

releases a resource back to the pool

(let [id _(pool:acquire |pool|)] (pool:release |pool| id)) => string?

pool:resources:busy

added in 3.0

(pool:resources:busy {:keys [state], :as pool})

returns all the busy resources

pool:resources:idle

added in 3.0

(pool:resources:idle {:keys [state], :as pool})

returns all the idle resources

pool:resources:thread

added in 3.0

(pool:resources:thread pool)(pool:resources:thread {:keys [state], :as pool} thread)

returns acquired resources for a given thread

pool:with-resource

macro

added in 3.0

(pool:with-resource [obj pool] & body)

takes an object from the pool, performs operation then returns it

pool?

added in 3.0

(pool? obj)

checks that object is a pool

(pool? |pool|) => true

pop

added in 3.0

(pop queue)

pops from the front of the queue

process-bulk

(process-bulk client inputs outputs)(process-bulk client inputs outputs opts)

process-bulk

added in 3.0

(process-bulk f queue maximum)

processes elements in the queue

(def +state+ (atom []))

(q/process-bulk (fn elems(swap! +state+ conj elems)) (q/queue 1 2 3 4 5) 3)

@+state+ => [1 2 3 4 5]

process-single

(process-single client output)(process-single client output opts)

push

added in 3.0

(push queue element)

puts at the front of the queue

put

added in 3.0

(put queue element)(put queue element timeout)(put queue element timeout timeunit)

puts an element at the back

(->> (doto (q/queue) (q/put 1)) (into [])) => 1

put-first

added in 3.0

(put-first queue element)(put-first queue element timeout)(put-first queue element timeout timeunit)

puts at the front of the queue

put-last

added in 3.0

(put-last queue element)(put-last queue element timeout)(put-last queue element timeout timeunit)

puts at the back of the queue

queue

added in 3.0

(queue)(queue & elements)

constructs a blocking queue

(q/queue 1 2 3) => java.util.concurrent.LinkedBlockingQueue

queue:fixed

added in 3.0

(queue:fixed size)

constructs a fixed size blocking queue

(type (q/queue:fixed 10)) => java.util.concurrent.ArrayBlockingQueue

queue:limited

added in 3.0

(queue:limited size)

constructs a limited queue

(q/queue:limited 10)

queue?

added in 3.0

(queue? obj)

checks if object is a BlockingQueue

relay

added in 3.0

(relay {:keys [type], :as m})

creates and starts a relay

relay:create

added in 4.0

(relay:create {:keys [id type], :as m})

creates a relay

remaining-capacity

added in 3.0

(remaining-capacity queue)

returns the remaining capacity

(q/remaining-capacity (q/queue)) => 2147483647

(q/remaining-capacity (doto (q/queue:fixed 2) (q/put 1))) => 1

remove

added in 3.0

(remove queue obj)

removes element from queue

req

macro

added in 3.0

(req client command)(req client command opts)

execute command on single, bulk and transact calls

(req |client| {:type :eval :form 1})

req-fn

added in 3.0

(req-fn client command)(req-fn client command opts)

function for req

req:applicative

added in 3.0

(req:applicative m)

constructs a request applicative

req:bulk

macro

added in 3.0

(req:bulk [client opts] & body)

creates a bulk request

req:command

added in 3.0

(req:command m)

constructs a command

req:in

macro

added in 3.0

(req:in & body)

captures the command input

req:opts

added in 3.0

(req:opts opts)(req:opts opts {:keys [pre post chain], :as m})

clean opts for processing inputs

req:opts-clean

added in 3.0

(req:opts-clean opts)

clean opts for processing inputs

req:run

added in 3.0

(req:run command client args)(req:run {:keys [options process], :as command} client args opts)

runs a command

req:transact

macro

added in 3.0

(req:transact [client opts] & body)

creates a bulk transaction request

request-bulk

(request-bulk client commands)(request-bulk client commands opts)

request-single

(request-single client command)(request-single client command opts)

schedule

added in 3.0

(schedule service f interval)(schedule service f interval {:keys [min]})

schedules task for execution

schedule:fixed-delay

added in 3.0

(schedule:fixed-delay service f delay)(schedule:fixed-delay service f delay {:keys [initial min]})

schedules task at fixed delay

schedule:fixed-rate

added in 3.0

(schedule:fixed-rate service f interval)(schedule:fixed-rate service f interval {:keys [initial min]})

schedules task at a fixed rate

send

added in 3.0

(send {:keys [instance], :as relay} msg)

sends command to relay

stacktrace

added in 3.0

(stacktrace)(stacktrace thread)

returns thread stacktrace

(stacktrace)

submit

added in 3.0

(submit service f)(submit service f {:keys [min max delay default], :as m})

submits a task to an executor

@(submit (executor:single) (fn []) {:min 100}) ^hidden

@(submit (executor:single) (fn [] (Thread/sleep 1000)) {:max 100}) => (throws)

submit-notify

added in 3.0

(submit-notify service f)(submit-notify service f {:keys [min max delay default], :as m})

submits a task (generally to a fixed size queue)

(doto (executor:single 1) (submit-notify (fn []) 1000) (submit-notify (fn []) 1000) (submit-notify (fn []) 1000))

take

added in 3.0

(take queue)(take queue timeout)(take queue timeout timeunit)

takes an element from the queue

((juxt q/take #(into [] %)) (q/queue 1 2 3)) => [1 2 3]

take-first

added in 3.0

(take-first queue)(take-first queue timeout)(take-first queue timeout timeunit)

takes from the front of the queue

take-last

added in 3.0

(take-last queue)(take-last queue timeout)(take-last queue timeout timeunit)

takes from the back of the queue

thread

added in 3.0

(thread {:keys [handler daemon priority classloader uncaught name start]})

creates a new thread

thread:active-count

added in 3.0

(thread:active-count)

returns active threads

thread:alive?

added in 3.0

(thread:alive? thread)

checks if thread is alive

thread:all

added in 3.0

(thread:all)

lists all threads

thread:all-ids

added in 3.0

(thread:all-ids)

lists all thread ids

thread:classloader

added in 3.0

(thread:classloader)(thread:classloader thread)(thread:classloader thread loader)

gets and sets the context classloader

thread:current

added in 3.0

(thread:current)

returns the current thread

thread:daemon?

added in 3.0

(thread:daemon?)(thread:daemon? thread)

checks if thread is a daemon

thread:dump

added in 3.0

(thread:dump)

dumps out current thread information

thread:global-uncaught

added in 3.0

(thread:global-uncaught)(thread:global-uncaught f)

gets and sets the global uncaught exception handler

thread:has-access?

added in 3.0

(thread:has-access? thread)

checks if thread allows access to current

thread:has-lock?

added in 3.0

(thread:has-lock? lock)

checks if thread has the lock

thread:id

added in 3.0

(thread:id)(thread:id thread)

returns the id of a thread

thread:interrupt

added in 3.0

(thread:interrupt thread)

interrupts a thread

thread:interrupted?

added in 3.0

(thread:interrupted? thread)

checks if thread has been interrupted

thread:join

added in 3.0

(thread:join)(thread:join thread)(thread:join thread millis)(thread:join thread millis nanos)

calls join on a thread

thread:notify

added in 3.0

(thread:notify lock)

notifies threads waiting on lock

thread:notify-all

added in 3.0

(thread:notify-all lock)

notifies all threads waiting on lock

thread:run

added in 3.0

(thread:run thread)

runs the thread function locally

thread:sleep

added in 3.0

(thread:sleep ms)

sleeps for n milliseconds

thread:spin

added in 3.0

(thread:spin)

waits using onSpin

thread:start

added in 3.0

(thread:start thread)

starts a thread

thread:uncaught

added in 3.0

(thread:uncaught)(thread:uncaught thread)(thread:uncaught thread f)

gets and sets the uncaught exception handler

thread:wait-on

added in 3.0

(thread:wait-on lock)

waits for a lock to notify

thread:yield

added in 3.0

(thread:yield)

calls yield on current thread

transact

added in 3.0

(transact client thunk)

enable transactions on client

(->> (transact |client| (fn [] (req |client| {:type :eval :form 1} {:chain #(* 8 %)}))) (map deref))

wrap-min-time

added in 3.0

(wrap-min-time f total)(wrap-min-time f total delay)

wraps a function with min-time and delay

((wrap-min-time (fn []) 20 0))

((wrap-min-time (fn []) 100 10))