简体   繁体   中英

how to avoid synchronization issue in writing contents to file - Java ( android)

I have a requirement which updates the file contents from multiple operation which can occur at the same time.

I need to queue each request and the change the contents of the file for each request one by one.

I am using android broadcast receiver which receives the events for changing the file contents.

How can I queue this events and make changes to file one after the other request.

Please provide some suggestion or if possible please provide any links that I can refer.

I came, several time ago, across a class used by Android internally to serve the purpose you want to reach and which is now available through the Android Support Library .

It's called AtomicFile , it is documented here and you can find its implementation here .

I have used executor with single thread. When event is received, we can post runnable to the executor. As the pool has 1 thead, these tasks will run in sequence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM