문제

I intend to parse a config file. This will happen once at the startup of my application. During it's execution the application has several threads. Each thread needs some of the parsed config parameters (but doesn't need to change them).

how can I do this with plain Java - economical of resources? Thanks a lot!

도움이 되었습니까?

해결책

Create the class that parses the config file as a singleton.

Parse the config before you start the other threads

Get the code in the threads to refer to the singleton object containing the parsed config.

다른 팁

You can create a class which will be the holder for configuration, parse it at static initializer (guaranteed to be thread safe and happen only once during class loading unless you will programatically load it via ClassLoader) and hold a static (and final needed) reference to the parsed config object representation. It seems to be quite economical.

Or you can just implement the classic singleton pattern for loader/holder class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top