vendredi 7 octobre 2016

singleton in multi threaded environment

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 multithreaded 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.

Please help , how to solve this problem. Is there any alternative design pattern to solve this. Thanks for your time .

Aucun commentaire:

Enregistrer un commentaire