How to properly handle two threads updating the same row in a database

前端 未结 3 2055
夕颜
夕颜 2020-12-13 15:08

I have a thread called T1 for reading a flat file and parsing it. I need to create a new thread called T2 for parsing some part of this file and la

3条回答
  •  攒了一身酷
    2020-12-13 15:26

    Assuming that each thread T1,T2 will parse different parts of the file, means no one override the other thread parsing. the best thing is to decouple your parsing process from the DB commit.

    T1, T2 will do the parsing T3 or Main Thread will do the commit after both T1,T2 has finished. and i think in this approach its more correct to change the file status to Parsed only when both threads has finished.

    you can think of T3 as CommitService class which wait till T1,T2 finsih and then commit to DB

    CountDownLatch is a helpful tool to do it. and here is an Example

提交回复
热议问题