; Bank (defstruct account-struct: ID: Owner: Balance-ref); account dimension (DEF account-Map-ref (sorted-map ))); save the collection of all accounts; create an account; @ ARGs: account ID; @ return: Account Information (defn open-Account "creates a new account, stores it in the account map and returns it "[owner] (dosync; required because a ref is being changed (let [Account-map @ account-Map-ref last-entry (last account-map) ID (if last-entry (INC (key last-entry) 1); if the last If an account exists, it is automatically added; otherwise, it is 1 account (struct account-struct ID owner (ref 0: validator integer?)]; Create an account Account-struct force ref digit type (alter account-Map-ref assoc ID account); update the newly created account-struct account to account-Map-ref account ); return to this account); save money; ARGs: Account Information amount; Return: nil (defn deposit [account amount] (dosync (thread/sleep 50 ); simulate a long-running operation (let [owner (account: owner); account balance-ref (account: Balance-ref); amount type (if (POS? Amount) "deposit" "Withdraw"); greater than 0 or less than 0 ction (if (POS? Amount) "to" "from") ABS-amount (Math/ABS amount)] (if (> = (+ @ balance-ref amount) 0 ); balance (do (alter balance-ref + amount); Modify balance cannot use commute (println (STR type "ing") ABS-amount direction owner )); print Information (throw (illegalargumentexception .; if the balance is insufficient, send a prompt and roll back the transaction (Str "insufficient balance for" owner "to withdraw" Abs-amount); get the money; ARGs: Account Information amount; return: nil (defn withdraw "removes money from an account" [account amount] (Deposit Account (-amount); Transfer; ARGs: The amount of the target account of the source account; return: nil (defn transfer [from-account to-account amount] (dosync (println "transferring" amount "from" (from-account: owner) "to" (to-account: owner) (withdraw from-account amount) (deposit to-account amount); Queries private functions of a single account; ARGs: account; return: nil (defn-report-pri-1 "prints information about a single account" [account] (let [balance-ref (account: Balance-ref)] (println "balance for" (account: owner) "is" @ balance-ref); Queries private functions of multiple accounts; ARGs: account list; return: nil (defn Report "prints information about any number of accounts" [accounts] (dosync (doseq [Account accounts] (report-pri-1 account); handle exceptions not handled by the current thread; ARGs: The current thread is abnormal; Return: nil (thread/setdefaultuncaughtexceptionhandler (proxy [thread $ uncaughtexceptionhandler] [] (uncaughtexception [thread throwable]; just print the message in the exception. (println (.. throwable. getcause. getmessage) "thread exception"); Call (let [Mark (open-Account "mark") Tami (open-Account "Tami") thread (thread. # (Transfer mark Tami 50)] (try (deposit mark 100); save 100 (deposit Tami 200); save 200 (. start thread); Transfer 50 (. join thread); wait for the sub-thread to exit; (withdraw mark 75); Take 75 (report [Mark Tami]); query (catch illegalargumentexception E (println (. getmessage E) "in main thread "))))