MCPcopy Index your code
hub / github.com/clojure/clojure / Agent

Class Agent

src/jvm/clojure/lang/Agent.java:22–287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20import java.util.concurrent.atomic.AtomicReference;
21
22public class Agent extends ARef {
23
24static class ActionQueue {
25 public final IPersistentStack q;
26 public final Throwable error; // non-null indicates fail state
27 static final ActionQueue EMPTY = new ActionQueue(PersistentQueue.EMPTY, null);
28
29 public ActionQueue( IPersistentStack q, Throwable error )
30 {
31 this.q = q;
32 this.error = error;
33 }
34}
35
36static final Keyword CONTINUE = Keyword.intern(null, "continue");
37static final Keyword FAIL = Keyword.intern(null, "fail");
38
39volatile Object state;
40 AtomicReference<ActionQueue> aq = new AtomicReference<ActionQueue>(ActionQueue.EMPTY);
41
42 volatile Keyword errorMode = CONTINUE;
43 volatile IFn errorHandler = null;
44
45final private static AtomicLong sendThreadPoolCounter = new AtomicLong(0);
46
47final private static AtomicLong sendOffThreadPoolCounter = new AtomicLong(0);
48
49volatile public static ExecutorService pooledExecutor =
50 Executors.newFixedThreadPool(2 + Runtime.getRuntime().availableProcessors(),
51 createThreadFactory("clojure-agent-send-pool-%d", sendThreadPoolCounter));
52
53volatile public static ExecutorService soloExecutor = Executors.newCachedThreadPool(
54 createThreadFactory("clojure-agent-send-off-pool-%d", sendOffThreadPoolCounter));
55
56final static ThreadLocal<IPersistentVector> nested = new ThreadLocal<IPersistentVector>();
57
58private static ThreadFactory createThreadFactory(final String format, final AtomicLong threadPoolCounter) {
59 return new ThreadFactory() {
60 public Thread newThread(Runnable runnable) {
61 Thread thread = new Thread(runnable);
62 thread.setName(String.format(format, threadPoolCounter.getAndIncrement()));
63 return thread;
64 }
65 };
66}
67
68public static void shutdown(){
69 soloExecutor.shutdown();
70 pooledExecutor.shutdown();
71}
72
73static class Action implements Runnable{
74 final Agent agent;
75 final IFn fn;
76 final ISeq args;
77 final Executor exec;
78
79

Callers

nothing calls this directly

Calls 2

internMethod · 0.95
createThreadFactoryMethod · 0.95

Tested by

no test coverage detected