L2TIntObjectHashMap.java 7.9 KB

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