Reputation:
I understand the very basics of the public
and private
keywords (I'm still not sure what protected
does)
I have a question regarding "protection of data members for thread safety."
For example, say I have a class MyClass
that extends Thread and belonging to this class is a private data member called MyDataMember
. Suppose there are public accessor and setter functions for this data member which make sure to synchronize the data member.
Now this is all fine from an "external" point of view in that other threads can only set and retrieve the data member when synchronized properly. However, what about other functions within MyClass
? Say I have another function within MyClass
called DoSomething
. I know I should not attempt to access MyDataMember
directly, but I might forget from time to time to call the accessor/setter methods and access/set the data member directly.
My question is: Is there a keyword I can use to declare function members exclusive access to certain data members? This way, if I "accidentally" directly access a data member from a method that does not have exclusive access, then an error would occur. I just think this would make things a bit safer during development (at least for me!)
Upvotes: 0
Views: 2473
Reputation: 4859
Regarding the protected
key word, read more here:
Short version: methods/fields marked protected
can be accessed by subclasses and by classes in the same package.
I think you have confused the terms (or I do not understand your question properly). There is no synchronization guaranteed just because you create a setter or a getter for a field in a class. However, if you add the synchronized
keyword to the method signature (public synchronized void doSomething()
), you are guaranteed there will be only one thread running the method at any given time. Other calls to doSomething()
will block until the thread running the method have left the method.
The volatile
keyword can be used if you want to make sure all thread sees the updated reference to a (for example) immutable object (object which cannot be changed after creation). If you have two threads modifying a public String value;
, the first thread might not see that the second thread has updated the reference if you do not have the volatile
keyword. Read more here: When exactly do you use the volatile keyword in Java?.
I'm not sure any of this would help in your example, as you are asking for "exclusive access" to a member variable. In general, I would try to avoid mixing active object (threads) with passive objects (data). Perhaps you could instead make the MyDataMember
thread-safe (perhaps by using synchronized
), and you will not need to care about how you access it?
Upvotes: 0
Reputation: 156
No, there isn't.
Protected allows a variable to be modified by classes that inherit the class.
If you want to protect your data put it in a separate class with private members, then you can only use setters and getters.
Upvotes: 4
Reputation: 11910
Volatile keyword hints about a variable to be accessed from different threads.
volatile int a;
You need to access using synchronized keyword to make a body(or a method) a thread-safe around the variable.
// in thread 1
sycnhronized(lockedItem)
{
a++; // just a++ alone may not be tread-safe because being non-atomic
}
// in thread 2
sycnhronized(lockedItem)
{
a--; // just a-- alone may not be tread-safe because being non-atomic
}
Upvotes: 1