How to lock a file on different application levels?

前端 未结 6 910
萌比男神i
萌比男神i 2021-01-05 17:01

Here\'s the scenario: I have a multi threaded java web application which is running inside a servlet container. The application is deployed multiple times inside the servlet

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 17:46

    Using java.nio.channels.FileLock, with a ReadWriteLock.

    If I were you, I'd hide the File, FileChannel and all FileOutputStream from all business code. Replaced with my own simple adapter class, like an DAO.

    e.g.

    abstract class MyWriter{
        private FileChannel file;
        public void writeSomething(byte[] b){
            // get VM scope write lock here
            // get file lock here
            // do write
            // release file lock
            // release readwritelock lock
        }
    }
    

提交回复
热议问题