0

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!)

  • Yes, the accessor/setter functions in `MyClass` use the synchronized keyword, which works fine. But I am wondering if there is a way to block direct access to `MyDataMember` from methods within `MyClass` other than the accessor/setter methods. –  Feb 23 '14 at 11:10
  • 1
    about your My question is part : Eg : private int i; accessor and setter/mutator are also methods (like any other non-static method). there is no way to prevent methods from directly accessing the variables of the same class. – TheLostMind Feb 23 '14 at 11:10
  • 2
    No, there is no way to do that. Maybe you want to ue an `AtomicReference`? – fge Feb 23 '14 at 11:10
  • Ok, thanks for the information. I didn't think there was, but wasn't sure. Would make development easier as the compiler could flag up illegal access to data members IMO. –  Feb 23 '14 at 11:13
  • 1
    In C++, the way to do this is to wrap the member you need to protect in its own class and that class simply doesn't permit you to access or modify the member unless you pass it a reference to your held lock. So there's no way to write code to access the member without a lock. – David Schwartz Feb 23 '14 at 11:22

3 Answers3

4

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.

yshavit
  • 42,327
  • 7
  • 87
  • 124
Tomas
  • 156
  • 7
  • This seems like one way to achieve what I'm after. Keep all data members to be accessed by different threads in one class only (as private members) and implement accessor/setter methods within that class only. Thanks. –  Feb 23 '14 at 11:22
  • Thread safety is unrelated to access modifiers. Restricting access makes it easier to ensure all access is thread safe, but this is something you still need to do. – Peter Lawrey Feb 23 '14 at 12:49
1

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


}
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
0

Protected keyword

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.

Synchronization

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?

Community
  • 1
  • 1
joscarsson
  • 4,789
  • 4
  • 35
  • 43
  • Thanks for the extra info. I suppose my question can be rephrased as "Can a function member be specified as having exclusive access to certain data members?" This way, only accessor/setter methods could be granted access to certain data members, which would IMO improve thread safety of data members within the classes the data members are defined. –  Feb 23 '14 at 11:49
  • For example, suppose there was an `exclusive` keyword. For data members `exclusive` would mean `private` and only accessible by `exclusive` function members. An `exclusive` function member would mean `public` and has access to `exclusive` data members. Non `exclusive` function members would not be permitted access to `exclusive` data members. –  Feb 23 '14 at 11:55
  • I see, but as you've been already answered there is no such keyword :) I think the Java solution would be to separate everything you want to have the `exclusive` keyword on into a separate object (as you are describing them there should be no communication between exlusive and non-exclusive data anyway). – joscarsson Feb 23 '14 at 12:02
  • 1
    @ joscarsson Yes. I wasn't sure though so wanted to check - I don't know everything about Java programming TBH ! :-) –  Feb 23 '14 at 12:06