2

I have a logging thread and a main app thread.

The main app runs every 50ms and has an usual tick length of 11ms to do all its things it needs to do. This 11ms or however long the tick lasted is logged in a variable in the main app in a public field.

In my logging thread i'd like to monitor the variable that keeps track of the average tick length in the main app thread to know when I should throttle the logging because main app flow will always have priority over logging. It is a public variable in the main app, unfortunately I have no control over the design of the main app and cannot make it a "thread safe" variable.

However I only want to read it and not write to it.

Can I safely read the variable directly(It doesn't really matter if the data is out of sync for a few microseconds or that i'm running behind on the data) or should I make a thread safe method that reads the variable for me and inject that in the main app via reflection?

What are the possible issues I should keep mind of? And what would be the best approach?

The intent of this question is to get an idea of all issues that could pop up in this scenario and what I should be mindful of. I'm not interested in a patch code solution, but more interested in learning the behaviour of threads and variables between threads and what pitfalls can be..

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 1
    what version of java you want to use? because java8 has more interesting features to solve your problem – Dmitry Zagorulkin Jul 01 '15 at 08:13
  • It needs to be compatible with java 7 at the minimum. – Tschallacka Jul 01 '15 at 08:29
  • possible duplicate of [Java reflection, add volatile modifier to private static field](http://stackoverflow.com/questions/27565619/java-reflection-add-volatile-modifier-to-private-static-field) – xmoex Jul 01 '15 at 08:40
  • @xmoex my question is not the same. The intent of this question is to find the best approach and what issues I should keep in mind. reflection is only one of the possible outcomes. And the field i'm discussing is not private, its public and its not static, which makes it an entirely different ballgame. – Tschallacka Jul 01 '15 at 08:42
  • I see your point. But did you consider the synchronizing approach in the accepted answer? – xmoex Jul 01 '15 at 09:57
  • Yes, as you can note I'm considering injecting a method via reflection/asm to read the vriable in a safe way. I just want to explore the behaviour and pro's and cons before doing such a modification. – Tschallacka Jul 01 '15 at 10:06

1 Answers1

1

You cannot assume that one thread's view of a variable is the same as another thread's view.

In the general case the system will cache the variable but this cache will be flushed to core memory often enough for you to see it almost always up-to-date. However, this is a dangerous assumption as there is no commitment to achieve that functionality in the JVM.

It would be perfectly possible to attempt to read the value from a different thread and never see it change.

For a reliable solution that is cross-platform and resilient to Java version you should find a different way to access the value - your thoughts on injection via reflection may be workable.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213