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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -