开发者

Question about practices of Java reference and synchronized keyword?

开发者 https://www.devze.com 2023-02-12 22:45 出处:网络
I\'m confused by an issue about reference and synchronized keyword a long time. I usually see some code like this:

I'm confused by an issue about reference and synchronized keyword a long time. I usually see some code like this:

Class someClass {
    ...
    private SomeObject mObject;
    ...
    public void someMethod() {
        ...
       final SomeObject obj = mObject;
       ...
       //then use the 'obj' variable rather than mObject
       ...
    }

}

My question is why should use local final variable obj to replace th开发者_Go百科e member variable? Why not use the member variable directly?

I also see some example code associated with 'synchronized' keyword,like this:

public void write(byte[] out) {
    // Create temporary object
    ConnectedThread r;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
        if (mState != STATE_CONNECTED) return;
        r = mConnectedThread;
    }
    // Perform the write unsynchronized
    r.write(out);
}

Why these code can achieve the synchronized goal? Thanks!


From what I can understand of your question and the first example, the code is trying to avoid threading problems by wanting to take a local copy of the member variable. The first example doesn't do this, it just gets a new local variable pointing to the same object the member variable points to, but doesn't actually protect invokations on that object from threading issues.

Edit following @Nick's comment: like Nick says, the first example's someMethod method avoids the possibility of having the mObject being replaced by another instance half way through. It doesn't however protect against threading issues by concurrent invokations on the same instance.


1> I think for ur first question...member variable is not used directly because that reference object can be used in some other methods as well....and in other methods it may not be required to be declared as final as in case of the method as u have shown....so it is good practice to create a local reference of that variable.....

2> Suppose some reference object is being accessed concurrently using threads....now if that object modifies some data then for concurrent access integrity of that data is lost....so it is required that there is some kind of lock on that object reference so that while one thread is accessing one of the reference of that object some other thread should not be able to access it....so this synchronized keyword is used to achieve just that...


In the first example, I believe the idea is to take a copy of the member variable in a final local variable so that if the member variable is changed on another thread, the copy in the member function will remain the same.

The second example is similar in that it's taking a copy of the currently connected thread in a local variable.

Imagine in either case if the member variable (or connected thread) were accessed directly and then changed part way through the function call by another thread, undefined behaviour may occur.

I'm sure there's a name for this coding pattern but I can't remember it!


As far as the first question is concerned it's making sure that the method can't modify the object, by aliasing it to a final variable you're ensuring you're not reassigning something later? I'm not sure...

The second question: this works because if mState is not connected then we return from the method and r.write() doesn't get executed. It uses the object itself as a lock.

0

精彩评论

暂无评论...
验证码 换一张
取 消