multithreading - Java concurrent writes from multiple threads to a single text file? -


i have multi-threaded java 7 program (a jar file) uses jdbc perform work (it uses fixed thread pool).

the program works fine , logs things progresses command shell console window (system.out.printf()) multiple concurrent threads.

in addition console output need add ability program write single plain ascii text log file - multiple threads.

the volume of output low, file relatively small log file, not data file.

can please suggest , relatively simple design/approach done using java 7 features (i dont have java 8 yet)?

any code samples appreciated.

thank much

edit:

i forgot add: in java 7 using files.newoutputstream() static factory method stated thread safe - according official java documentation. simplest option write single shared text log file multiple threads?

if want log output, why not use logging library, e.g. log4j2? allow tailor log specific needs, , can log without synchronizing threads on stdout (you know running system.out.print involves locking on system.out?)

edit: latter, if things log thread-safe, , ok adding lmax' disruptor.jar build, can configure async loggers (just add "async") have logging thread take care of whole message formatting , writing (and keeping log messages in order) while allowing threads run on without hitch.


Comments