01: /*
02: * <copyright>
03: * Copyright 1997-2003 BBNT Solutions, LLC
04: * under sponsorship of the Defense Advanced Research Projects Agency (DARPA).
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the Cougaar Open Source License as published by
08: * DARPA on the Cougaar Open Source Website (www.cougaar.org).
09: *
10: * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
11: * PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
12: * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
14: * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
15: * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
16: * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
17: * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18: * PERFORMANCE OF THE COUGAAR SOFTWARE.
19: * </copyright>
20: */
21:
22: package org.cougaar.core.thread;
23:
24: /**
25: * The serializing trivial implementation of Thread Service. It
26: * consists of a single {@link SerialThreadRunner} and {@link
27: * SerialThreadQueue}.
28: */
29: final class SingleThreadServiceProxy extends TrivialThreadServiceProxy {
30: private static final int NUMBER_OF_RUNNERS = 1;
31:
32: private SerialThreadQueue queue;
33:
34: private SerialThreadRunner[] runners;
35:
36: /**
37: * Initialize the data structures used to store thread runners.
38: */
39: private void initialize() {
40: queue = new SerialThreadQueue();
41: runners = new SerialThreadRunner[NUMBER_OF_RUNNERS];
42: for (int i = 0; i < runners.length; i++)
43: runners[i] = new SerialThreadRunner(queue);
44: }
45:
46: /**
47: * Clean up the data structures so they can be reclaimed by the GC.
48: */
49: protected void unload() {
50: queue = null;
51: for (int i = 0; i < runners.length; i++) {
52: runners[i].stop();
53: // Aggressively nullify for GC.
54: runners[i] = null;
55: }
56: runners = null;
57: }
58:
59: SingleThreadServiceProxy() {
60: initialize();
61: }
62:
63: public Schedulable getThread(Object consumer, Runnable runnable,
64: String name) {
65: return new SerialSchedulable(runnable, name, consumer, queue);
66: }
67:
68: int iterateOverThreads(ThreadStatusService.Body body) {
69: int count = 0;
70: for (int i = 0; i < runners.length; i++)
71: count += runners[i].iterateOverThreads(body);
72: count += queue.iterateOverThreads(body);
73: return count;
74: }
75:
76: }
|