Java Question

If something doesn't fit in any other forum then post it here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
LeafaR
Posts: 13
Joined: Wed Dec 26, 2007 5:35 am

Java Question

Post by LeafaR »

Hi, I want to make a Java Question

Code: Select all

new FastMap<String, L2PcInstance>().setShared(true);
What does that setShared means? I goggle it, but nothing that explains came out, you guys are the one who uses it, so can you explain to me?
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: Java Question

Post by JIV »

Means Map will be thread safe.
User avatar
Pere
Posts: 400
Joined: Sat Jan 05, 2008 11:09 am
Location: Catalunya, Berguedà

Re: Java Question

Post by Pere »

..so there won't be collisions (mainly key collisions) if it is used in different threads (concurrent processes)
Bones tardes amics meus tots!
User avatar
DrHouse
L2j Inner Circle
L2j Inner Circle
Posts: 912
Joined: Mon Jan 22, 2007 12:14 am
Location: Spain

Re: Java Question

Post by DrHouse »

Here is an example in method put

Code: Select all

private final Object put(Object key, Object value, int keyHash,            boolean concurrent, boolean noReplace, boolean returnEntry) {        final FastMap map = getSubMap(keyHash);        final Entry[] entries = map._entries; // Atomic.        final int mask = entries.length - 1;        int slot = -1;        for (int i = keyHash >> map._keyShift;; i++) {            Entry entry = entries[i & mask];            if (entry == null) {                slot = slot < 0 ? i & mask : slot;                break;            } else if (entry == Entry.NULL) {                slot = slot < 0 ? i & mask : slot;            } else if ((key == entry._key) || ((keyHash == entry._keyHash) && (_isDirectKeyComparator ? key.equals(entry._key)                    : _keyComparator.areEqual(key, entry._key)))) {                if (noReplace) {                    return returnEntry ? entry : entry._value;                }                Object prevValue = entry._value;                entry._value = value;                return returnEntry ? entry : prevValue;            }        }         // Add new entry (synchronize if concurrent).        if (concurrent) {            synchronized (this) {                return put(key, value, keyHash, false, noReplace, returnEntry);            }        }         // Setup entry.        final Entry entry = _tail;        entry._key = key;        entry._value = value;        entry._keyHash = keyHash;        if (entry._next == null) {            createNewEntries();        }        entries[slot] = entry;        map._entryCount += ONE_VOLATILE; // Prevents reordering.        _tail = _tail._next;         if (map._entryCount + map._nullCount > (entries.length >> 1)) { // Table more than half empty.            map.resizeTable(_isShared);        }        return returnEntry ? entry : null;    }
See how argument "concurrent" (== isShared) affects method calling it within synchronized block. BTW pm forsaiken for further info he is a real expert on this :D
Image

Leadership and management are not talk and talk, but talk and do

Proud of being a part of this project
User avatar
ZaKaX
Posts: 357
Joined: Thu Nov 22, 2007 6:28 am
Location: Somewhere in Asia.

Re: Java Question

Post by ZaKaX »

DrHouse wrote:BTW pm forsaiken for further info he is a real expert on this :D
:) Tah ub3r Rofl0per
¿ Que dice los cojones de la nina ?
Vapulabe
Posts: 271
Joined: Wed Mar 19, 2008 10:16 am

Re: Java Question

Post by Vapulabe »

FastMap is not part of Java but comes from one of the libraries used with L2J (javolution) You may find Javolution doc at http://javolution.org/target/site/apidocs/index.html (javadoc documentation). Look in bottom left frame for FastMap.
FastMap has a predictable iteration order, which is the order in which keys are inserted into the map (similar to java.util.LinkedHashMap collection class). If the map is marked shared then all operations are thread-safe including iterations over the map's collections. Unlike ConcurrentHashMap, access never blocks; retrieval reflects the map state not older than the last time the accessing threads have been synchronized (for multi-processors systems synchronizing ensures that the CPU internal cache is not stale). In most application it is not a problem because thread synchronization is done at high level (e.g. scheduler) and threads run freely (and quickly) until the next synchronization point. In some cases the "happen before" guarantee is necessary (e.g. to ensure unicity) and threads have to be synchronized explicitly. Whenever possible such synchronization should be performed on the key object itself and the not the whole map. For example:

Code: Select all

      FastMap<Index, Object> sparseVector = new FastMap<Index, Object>().setShared(true)     ... // Put     synchronized(index) { // javolution.util.Index instances are unique.         sparseVector.put(index, value);     }     ... // Get     synchronized(index) { // Blocking only when accessing the same index.         value = sparseVector.get(index); // Latest value guaranteed.     }
Post Reply