1

I Have a Singleton class called FILELOGGER and property called number_of_lines. I will make sure only one object is able to create for FILELOGGER class which makes singleton.

Throughout my application, my object is able to write it to a file and update number_of_lines property for each write.

What if i use this design pattern in multi threaded environment. How it behaves and i feel like number_of_lines property should be locked when other threads are trying to update. And I might to loose logging of data with delay and performance will be loosing.

Say for example thread T1 is logging at time 10:10:10 and T2 is also logging at the same exact time and both trying to update number_of_lines property.

How to solve this problem? Is there any alternative design pattern to solve this. Thanks for your time.

7
  • " how it behaves and i feel like number_of_lines property should be locked when other threads are trying to update. and i might to loose logging of data with delay and performance will be loosing." not sure what you mean here yet. But I think you have to lock both the writeToFile function (or your file will be messed up) and the field number_of_lines to make value atomic. Commented Oct 8, 2016 at 4:39
  • yes. if i lock both writeToFile and field number_of_lines . i will end with loosing performance as other write operations will be waiting in queue and will not able to log at exact time Commented Oct 8, 2016 at 4:43
  • OK I will give you 2 solution: 1. include the time in message. 2. Write to multiple file. (losing not loosing btw) Commented Oct 8, 2016 at 6:22
  • i think its a bad approach to write to multiple files for each thread associated and then run a algorithm to merge these files . messy would it be. isn't it. Commented Oct 8, 2016 at 6:46
  • What do you exactly want: Keep thevariables in a consistent state? Be able to determine which event happened before the other? Mitigate performance bottlenecks? Commented Oct 8, 2016 at 7:33

2 Answers 2

0

You can either synchronize access to the whole file as you've already done, or there's an alternative with some cons: snapshotting.

  • N threads write file contents to a string variable.
  • A dedicated thread does snapshots of in-memory data to disk and updates number_of_lines. number_of_lines will be synchronized when this dedicated thread needs to update it. Snapshotting may occur in time intervals like 10 seconds, 1 minute, 1 hour...

The main issue with this approach is an application/system crash will mean losing the data that wasn't persisted to disk since the last snapshot, but since your application works with in-memory data, it should increase overall performance.

Sign up to request clarification or add additional context in comments.

Comments

0

Also you have to implement the singleton pattern in a thread safe way. I think the best approach is with an inner class to guarantee the purpose of the singleton in a multi threaded application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.