Multithread Static Variable

How can I access a static variable from a large stream at the same time.

If I have a class like

Class A { public static boolean FLG=false; ..................... .................... } 

And I need to access the value from stream 1, for example

 .................... public void run() { boolean t1=A.FLG; .................. } 

and from stream 2 I need to set the value as

 .................... public void run() { A.FLG=true; .................. } 

Does this cause a memory impairment ?. If so, what is the recommended method to deal with this situation?

+4
source share
5 answers

Wrap the static variable with a synchronous method and call the method as you like

 public static synchronized void method1(){ //Whatever } public static synchronized void method2(){ //Whatever again } 

Please note that there are other ways to synchronize access to the method. They are considered more efficient in thread-busy environments by resorting to the same methods.

Check out the ReentrantLock class. There are also answers to when you use synchronized and RentrantLock and much more information that can be found through google.

Just like peter answer and muel comment. Marking the boolean variable as mutable should be helpful. volatile Boolean variables will NOT cache the initial value ( false or true ). The JVM may do this sometimes, which may be unexpected for the programmer.

+3
source

If all you want to do is get and install the primitive, you can make it volatile and it will be thread safe for these operations.

+3
source

In class A, you can create a set and get method for FLG, for example:

 public static synchronized boolean getFlag() { return FLG; } public static synchronized setFlag(boolean flag) { FLG=flag; } 

Now from other threads, the FLG access value uses this method. This will keep the FLG value consistent for multiple threads.

+1
source

You may get some undesirable situation when two threads try to set different values ​​to a static variable, and you are not sure what exactly this value really is. The best way (thinking in a simple scenario) I think it uses AtomicBoolean ( http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html ) and you get value in the object and use it (instead of using the object all the time, because another thread can change it, and you can get an unexpected script).

Another suggestion is to use Byteman to create parallel tests.

Regards, Luan

+1
source

If you do not want to use a synchronized, ReentrantLock, you can write your own logic for this.

Example:

 public class A extends Thread{ public static boolean FLG=false; public A(String threadName) { start(); setName(threadName); } @Override public void run() { // TODO Auto-generated method stub while(true){ if(this.getName().equals("getterThread") && FLG == true){ boolean t1=A.FLG; } if(this.getName().equals("setterThread") && FLG == false){ A.FLG = true; } } } public static void main(String[] args) { A dad = new A("getterThread"); A son = new A("setterThread"); } } 
0
source

Source: https://habr.com/ru/post/1499365/


All Articles