[concurrency-interest] Thread-safe iterator

chakravarthy.varaga at wipro.com chakravarthy.varaga at wipro.com
Fri Feb 17 01:42:34 EST 2012


	Is there a guarantee that your protected final long resetToLastKey() is not accessed outside your iteration and your concrete next() implementation isn't accessed outside the transaction?


-----Original Message-----
From: concurrency-interest-bounces at cs.oswego.edu [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Johannes.Lichtenberger
Sent: Friday, February 17, 2012 11:26 AM
To: concurrency-interest at cs.oswego.edu
Subject: [concurrency-interest] Thread-safe iterator


I want to write a thread safe iterator for a DescendantAxis which returns all descendants of a node in a tree-structure.

All movement is done within a transaction (the mRtx instance):

    public synchronized final long nextNode() {
        synchronized (mRtx) {
            long retVal = -1;
            if (hasNext()) {
                retVal = next();
            return retVal;

The "iterator" returns all unique nodeIDs. hasNext() and next() are implementations of the Iterable<T> interface. As I'm synchronizing on the transaction reference I think it's safe (mRtx is also declared "final"). Should be safe even if hasNext() and next() are not synchronized as the first thing is a movement in hasNext():

     * Make sure the transaction points to the node after the last {@code hasNext()}.
     * This must be called first in {@code hasNext()}.
     * @return key of node where transaction was after the last call of
     *         {@code hasNext()}
    protected final long resetToLastKey() {
        // No check because of IAxis Convention 4.
        mNext = true;
        return mKey;

Did I miss something?

kind regards,

Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu

Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. 


More information about the Concurrency-interest mailing list