geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wendy Feng (JIRA)" <j...@apache.org>
Subject [jira] Created: (GERONIMO-5490) synchronized setter method but unsynchronized getter method on field members in class DownloadResults
Date Mon, 02 Aug 2010 03:42:15 GMT
synchronized setter method but unsynchronized getter method on field members in class DownloadResults
-----------------------------------------------------------------------------------------------------

                 Key: GERONIMO-5490
                 URL: https://issues.apache.org/jira/browse/GERONIMO-5490
             Project: Geronimo
          Issue Type: Bug
      Security Level: public (Regular issues)
          Components: Plugins
    Affects Versions: 2.1.5
            Reporter: Wendy Feng


Though JLS guarantees that 32-bit reads will be atomic, synchronized setter methods and unsynchronized
similarly-named getter methods are not proper. Unless the fields in question are declared
volatile, the Java Memory Model doesn't guarantee up-to-date field value visible to threads
in the absent of synchronization. Caller of unsynchronized getter  method could read stale
or inconsistent values, which could cause serious problems.

Here's the synchronized stter methods and unsynchronized similarly-named getter methods found
in the class DownloadResults:
public class DownloadResults implements Serializable, DownloadPoller {
...
    public synchronized void setCurrentFile(String currentFile) {
        this.currentFile = currentFile;
    }

    public synchronized void setCurrentMessage(String currentMessage) {
        this.currentMessage = currentMessage;
    }

    public synchronized void setCurrentFilePercent(int currentFileProgress) {
        this.currentFileProgress = currentFileProgress;
    }

    public synchronized void setFailure(Exception failure) {
        this.failure = failure;
    }
....
    public Exception getFailure() {
        return failure;
    }
    public String getCurrentFile() {
        return currentFile;
    }

    public String getCurrentMessage() {
        return currentMessage;
    }

    public int getCurrentFilePercent() {
        return currentFileProgress;
    }
...
}

I suggest it would be better to write this instead:

public class DownloadResults implements Serializable, DownloadPoller {
...
....
    public synchronized Exception getFailure() {
        return failure;
    }
    public synchronized  String getCurrentFile() {
        return currentFile;
    }

    public synchronized String getCurrentMessage() {
        return currentMessage;
    }

    public synchronized  int getCurrentFilePercent() {
        return currentFileProgress;
    }
...
}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message