2
0

L2TIntObjectHashMap.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.util;
  16. import gnu.trove.function.TObjectFunction;
  17. import gnu.trove.map.hash.TIntObjectHashMap;
  18. import gnu.trove.procedure.TIntObjectProcedure;
  19. import gnu.trove.procedure.TIntProcedure;
  20. import gnu.trove.procedure.TObjectProcedure;
  21. import java.util.concurrent.locks.Lock;
  22. import java.util.concurrent.locks.ReentrantReadWriteLock;
  23. /**
  24. * Custom extension of TIntObjectHashMap that is synchronized via ReentrantReadWriteLock.<br>
  25. * The purpose of this map is to replace the use of FastMap<K,V>.shared() which requires a lot of resources.
  26. * @author Nik
  27. * @param <V> value object.
  28. */
  29. public class L2TIntObjectHashMap<V extends Object> extends TIntObjectHashMap<V>
  30. {
  31. private static final long serialVersionUID = 1L;
  32. private final Lock _readLock;
  33. private final Lock _writeLock;
  34. private boolean _tempWritesLockDisable;
  35. public L2TIntObjectHashMap()
  36. {
  37. super();
  38. ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  39. _readLock = lock.readLock();
  40. _writeLock = lock.writeLock();
  41. _tempWritesLockDisable = false;
  42. }
  43. @Override
  44. public V put(int key, V value)
  45. {
  46. _writeLock.lock();
  47. try
  48. {
  49. return super.put(key, value);
  50. }
  51. finally
  52. {
  53. _writeLock.unlock();
  54. }
  55. }
  56. /**
  57. * Unsynchronized operation, its free from any locks.<br>
  58. * Its useful while the readLock is taken by a thread<br>
  59. * (forEach operation for example)<br>
  60. * and you need to put something in the map without causing a deadlock<br>
  61. * by taking the writeLock before the readLock is unlocked.
  62. * @param key
  63. * @param value
  64. * @return
  65. */
  66. public V unsynchronizedPut(int key, V value)
  67. {
  68. return super.put(key, value);
  69. }
  70. @Override
  71. public V get(int key)
  72. {
  73. if (!_tempWritesLockDisable)
  74. {
  75. _readLock.lock();
  76. }
  77. try
  78. {
  79. return super.get(key);
  80. }
  81. finally
  82. {
  83. if (!_tempWritesLockDisable)
  84. {
  85. _readLock.unlock();
  86. }
  87. }
  88. }
  89. @Override
  90. public void clear()
  91. {
  92. _writeLock.lock();
  93. try
  94. {
  95. super.clear();
  96. }
  97. finally
  98. {
  99. _writeLock.unlock();
  100. }
  101. }
  102. @Override
  103. public V remove(int key)
  104. {
  105. if (!_tempWritesLockDisable)
  106. {
  107. _writeLock.lock();
  108. }
  109. try
  110. {
  111. return super.remove(key);
  112. }
  113. finally
  114. {
  115. if (!_tempWritesLockDisable)
  116. {
  117. _writeLock.unlock();
  118. }
  119. }
  120. }
  121. /**
  122. * Unsynchronized operation, its free from any locks.<br>
  123. * Its useful while the readLock is taken by a thread
  124. * (forEach operation for example)<br>
  125. * and you need to remove something in the map without causing a deadlock<br>
  126. * by taking the writeLock before the readLock is unlocked.
  127. * @param key
  128. * @return
  129. */
  130. public V unsynchronizedRemove(int key)
  131. {
  132. return super.remove(key);
  133. }
  134. @Override
  135. public boolean equals(Object other)
  136. {
  137. _readLock.lock();
  138. try
  139. {
  140. return super.equals(other);
  141. }
  142. finally
  143. {
  144. _readLock.unlock();
  145. }
  146. }
  147. @Override
  148. public V[] values()
  149. {
  150. _readLock.lock();
  151. try
  152. {
  153. return super.values();
  154. }
  155. finally
  156. {
  157. _readLock.unlock();
  158. }
  159. }
  160. @Override
  161. public V[] values(V[] arg0)
  162. {
  163. _readLock.lock();
  164. try
  165. {
  166. return super.values(arg0);
  167. }
  168. finally
  169. {
  170. _readLock.unlock();
  171. }
  172. }
  173. @Override
  174. public int[] keys()
  175. {
  176. _readLock.lock();
  177. try
  178. {
  179. return super.keys();
  180. }
  181. finally
  182. {
  183. _readLock.unlock();
  184. }
  185. }
  186. @Override
  187. public int[] keys(int[] arg0)
  188. {
  189. _readLock.lock();
  190. try
  191. {
  192. return super.keys(arg0);
  193. }
  194. finally
  195. {
  196. _readLock.unlock();
  197. }
  198. }
  199. @Override
  200. public boolean contains(int val)
  201. {
  202. _readLock.lock();
  203. try
  204. {
  205. return super.contains(val);
  206. }
  207. finally
  208. {
  209. _readLock.unlock();
  210. }
  211. }
  212. @Override
  213. public boolean containsValue(Object arg0)
  214. {
  215. _readLock.lock();
  216. try
  217. {
  218. return super.containsValue(arg0);
  219. }
  220. finally
  221. {
  222. _readLock.unlock();
  223. }
  224. }
  225. @Override
  226. public boolean containsKey(int key)
  227. {
  228. _readLock.lock();
  229. try
  230. {
  231. return super.containsKey(key);
  232. }
  233. finally
  234. {
  235. _readLock.unlock();
  236. }
  237. }
  238. @Override
  239. public boolean forEachKey(TIntProcedure procedure)
  240. {
  241. _readLock.lock();
  242. try
  243. {
  244. return super.forEachKey(procedure);
  245. }
  246. finally
  247. {
  248. _readLock.unlock();
  249. }
  250. }
  251. /**
  252. * A safe from deadlock loop.<br>
  253. * put and remove synchronizers are disabled while this loop is running.<br>
  254. * Keep in mind that this uses writeLock instead of readLock,<br>
  255. * and its intended only if you are trying to put/remove something while looping<br>
  256. * the values of this map.
  257. * @param procedure
  258. * @return
  259. */
  260. public boolean safeForEachKey(TIntProcedure procedure)
  261. {
  262. _writeLock.lock();
  263. try
  264. {
  265. _tempWritesLockDisable = true;
  266. return super.forEachKey(procedure);
  267. }
  268. finally
  269. {
  270. _tempWritesLockDisable = false;
  271. _writeLock.unlock();
  272. }
  273. }
  274. @Override
  275. public boolean forEachValue(TObjectProcedure<? super V> arg0)
  276. {
  277. _readLock.lock();
  278. try
  279. {
  280. return super.forEachValue(arg0);
  281. }
  282. finally
  283. {
  284. _readLock.unlock();
  285. }
  286. }
  287. /**
  288. * A safe from deadlock loop.<br>
  289. * put and remove synchronizers are disabled while this loop is running.<br>
  290. * Keep in mind that this uses writeLock instead of readLock,<br>
  291. * and its intended only if you are trying to put/remove something while looping<br>
  292. * the values of this map.
  293. * @param arg0
  294. * @return
  295. */
  296. public boolean safeForEachValue(TObjectProcedure<V> arg0)
  297. {
  298. _writeLock.lock();
  299. try
  300. {
  301. _tempWritesLockDisable = true;
  302. return super.forEachValue(arg0);
  303. }
  304. finally
  305. {
  306. _tempWritesLockDisable = false;
  307. _writeLock.unlock();
  308. }
  309. }
  310. @Override
  311. public boolean forEachEntry(TIntObjectProcedure<? super V> arg0)
  312. {
  313. _readLock.lock();
  314. try
  315. {
  316. return super.forEachEntry(arg0);
  317. }
  318. finally
  319. {
  320. _readLock.unlock();
  321. }
  322. }
  323. /**
  324. * A safe from deadlock loop.<br>
  325. * put and remove synchronizers are disabled while this loop is running.<br>
  326. * Keep in mind that this uses writeLock instead of readLock,
  327. * <br>and its intended only if you are trying to put/remove something while looping<br>
  328. * the values of this map.
  329. * @param arg0
  330. * @return
  331. */
  332. public boolean safeForEachEntry(TIntObjectProcedure<V> arg0)
  333. {
  334. _writeLock.lock();
  335. try
  336. {
  337. _tempWritesLockDisable = true;
  338. return super.forEachEntry(arg0);
  339. }
  340. finally
  341. {
  342. _tempWritesLockDisable = false;
  343. _writeLock.unlock();
  344. }
  345. }
  346. @Override
  347. public boolean retainEntries(TIntObjectProcedure<? super V> arg0)
  348. {
  349. _writeLock.lock();
  350. try
  351. {
  352. return super.retainEntries(arg0);
  353. }
  354. finally
  355. {
  356. _writeLock.unlock();
  357. }
  358. }
  359. @Override
  360. public void transformValues(TObjectFunction<V, V> arg0)
  361. {
  362. _writeLock.lock();
  363. try
  364. {
  365. super.transformValues(arg0);
  366. }
  367. finally
  368. {
  369. _writeLock.unlock();
  370. }
  371. }
  372. }