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!

天蓬老师天蓬老师2596 days ago861

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-05-17 10:03:08

    我认为这样还是不行的,本质上Spring只是帮你管理实例,线程安全的问题还是要自己解决

    再者,你说的是多进程写文件,通过rpc也仅仅是多线程方式吧?

    其实本质就是想实现一个线程安全的文件写接口,单例是OK的,自己的实例当作锁就行了

    reply
    0
  • 滿天的星座

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

    =_=! 一个多进程的程序?

    reply
    0
  • 習慣沉默

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

    用zookeeper,zookepper一个特点是写同一个znode只能有一个客户端(进程)写入成功,就用这个客户端写文件,或者利用数据库写入同一个主键只有一个可以插入成功的特点

    reply
    0
  • Cancelreply