1 28 29 package com.caucho.util; 30 31 import java.util.ArrayList ; 32 33 53 public class Exit { 54 static private ThreadLocal <Queue> _waiting = new ThreadLocal <Queue>(); 55 56 private Exit() {} 57 58 68 static public void addExit(ExitListener listener, Object object) 69 { 70 Queue queue = _waiting.get(); 71 72 if (queue != null) 73 queue.add(listener, object); 74 } 75 76 83 static public boolean addExit() 84 { 85 Queue queue = _waiting.get(); 86 87 if (queue == null) { 88 queue = Queue.allocate(); 89 _waiting.set(queue); 90 return true; 91 } else 92 return false; 93 } 94 95 98 static public void exit() 99 { 100 Queue queue = null; 101 102 queue = _waiting.get(); 103 _waiting.set(null); 104 105 if (queue == null) 106 return; 107 108 int size = queue._listeners.size(); 109 for (int i = 0; i < size; i++) { 110 ExitListener listener = queue._listeners.get(i); 111 Object object = queue._objects.get(i); 112 113 if (listener != null) { 114 try { 115 listener.handleExit(object); 116 } catch (Exception e) { 117 } 118 } 119 } 120 121 queue.free(); 122 } 123 124 private static class Queue { 125 static Queue _freeList; 126 127 Queue _next; 128 ArrayList <ExitListener> _listeners = new ArrayList <ExitListener>(); 129 ArrayList <Object > _objects = new ArrayList <Object >(); 130 131 private Queue() 132 { 133 } 134 135 void add(ExitListener listener, Object object) 136 { 137 _listeners.add(listener); 138 _objects.add(object); 139 } 140 141 static Queue allocate() 142 { 143 if (_freeList == null) 144 return new Queue(); 145 146 Queue queue = _freeList; 147 _freeList = _freeList._next; 148 149 return queue; 150 } 151 152 void free() 153 { 154 _listeners.clear(); 155 _objects.clear(); 156 _next = _freeList; 157 _freeList = this; 158 } 159 } 160 } 161 | Popular Tags |