Clojure variables are immutable, but with the use of an agent, the storage location can be mutated alongside the use of functions. The changed state of the agent is then returned, as we'll see in the example below.
shutdown-agents
method?The shutdown-agents
method is used to stop all running agents in a code because agents make use of state and are continuously active.
(shutdown-agents)
The shutdown-agents
method receives no parameter.
The shutdown-agents
method has no return value as it is like a switch used to stop active agents.
(ns clojure.examples.example(:gen-class))(defn func [](def names (agent 10))(println @names)(send names + 200)(await-for 50 names)(println @names)(shutdown-agents))(func)
func
.names
variable with the agent
method and we set the agent state to 10
.names
variable.send
method to change the value of the names
variable.await-for
method to cause a delay of 50
seconds so that the send
method executes properly since it takes time to process.names
. Note that the names
variable is now 210
instead of 10
because of the help of the send
method.Note: To learn more about the
shutdown-agents
method, click here.
shutdown-agents
method all the agents are stopped. If not, an error will be thrown. You can try removing the line with the shutdown-agents
code.func
.RELATED TAGS
CONTRIBUTOR