multithreading - How to synchronize access to class field in Java? -
there variable in class, actively used multiple threads. example:
public class { private int magicvar; private void somemethod() { // operations magicvar } public synchronized void somepublicmethod() { // operations magicvar } private class b extends thread { @override private void run() { // operations magicvar } } }
how organize access variable in case? describe private synchronized setter , getter? or mark field violate?
that depends on how plan use class. synchronization not implementation detail, part of design.
consider using atomicinteger
when protecting access integer.
Comments
Post a Comment