search

Home  >  Q&A  >  body text

java - Singleton service solves multi-process writing files

Now we need a multi-process file writing program

If you write directly, you need to ensure that the process locks the file handle

Now I want to register a singleton writing service through spring

When multiple programs write files, they request the spring interface to write files

Is this solution feasible?

Or use a singleton RPC service?

A single instance can ensure that it is an instance when writing a file, avoiding multi-thread and multi-process synchronization problems!

天蓬老师天蓬老师2775 days ago1015

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-05-17 10:03:08

    I think this is still not possible. Essentially, Spring only manages instances for you, and you still have to solve the issue of thread safety yourself

    Furthermore, you are talking about writing files through multiple processes. RPC is only a multi-threading method, right?

    In fact, the essence is to implement a thread-safe file writing interface. Singleton is OK, just use your own instance as a lock

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-17 10:03:08

    =_=! A multi-process program?

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-17 10:03:08

    Using zookeeper, a characteristic of zookepper is that only one client (process) can successfully write to the same znode. Use this client to write files, or use the database to write the same primary key. Only one can insert successfully

    reply
    0
  • Cancelreply