L2TIntObjectHashMap.java 7.4 KB

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