摘要:序并发中的锁文件模式是企业设计模式中的一种。可以是本地锁,也可以分布式锁,看文件系统是本地还是分布式的,算是一种比较古老的方式。代码是原子操作要点检查文件如果不存在则创建,要是原子操作,使用即可实现锁占用过长怎么处理或者忘记解锁怎么处理
序
并发中的锁文件模式是Java企业设计模式中的一种。可以是本地锁,也可以分布式锁,看文件系统是本地还是分布式的,算是一种比较古老的方式。利用zk实现分布式锁,其实跟这个也比较类似。zk其本质是个树形结构。
代码</>复制代码
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Subclass of RandomAccessFile that always ensures that it
* has exclusive access to a file before opening it.
*/
public class ExclusiveRandomAccessFile extends RandomAccessFile {
private static final String LOCK_FILE_SUFFIX = ".lck";
private File lockFile;
/**
* Open the named file using a lock file to ensure
* exclusive access.
* @param fileName The name of the file to open.
* @param mode This should either be "r" for read-only
* access or "rw" for read-write access.
* @exception FileSharingException
* If there is already a lock file for the named
* file.
* @exception IOException
* If there is a problem opening the file
*/
public static ExclusiveRandomAccessFile openExclusive(String fileName,
String mode)
throws IOException {
File lockFile = new File(fileName+LOCK_FILE_SUFFIX);
//createNewFile是原子操作
if (!lockFile.createNewFile()) {
// lock file already exists
throw new FileSharingException(fileName);
} // if
return new ExclusiveRandomAccessFile(fileName,
mode,
lockFile);
} // openExclusive(String)
/**
* Construct a RandomAccessFile that has exclusive access
* to the given file.
* @param fileName The name of the file to open.
* @param mode This should either be "r" for read-only
* access or "rw" for read-write access.
* @param lockFile A file object that identifies the lock
* file.
*/
private ExclusiveRandomAccessFile(String fileName,
String mode,
File lockFile)
throws IOException {
super(fileName, mode);
this.lockFile = lockFile;
} // constructor(String, String)
/**
* Close this file.
*/
public synchronized void close() throws IOException {
if (lockFile!=null) { // If file is still open
lockFile.delete();
lockFile = null;
super.close();
} // if
} // close()
}
class FileSharingException extends IOException {
public FileSharingException(String msg) {
super(msg);
} // constructor(String)
} // class FileSharingException
要点
1、检查文件如果不存在则创建,要是原子操作,使用File.createNewFile即可实现
2、锁占用过长怎么处理?或者忘记解锁怎么处理?
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65801.html
摘要:离线并发多个数据库事务中支持多线程的各种应用服务器并发问题丢失更新同时编辑文件,相继保存,最终丢失先保存者更新的内容不一致性读取期间,数据有更新执行语境从与外界交互角度看的个语境请求对应于软件工作的外部环境发出的单个调用,处理请求的软件会决 离线并发:多个数据库事务中支持多线程的各种应用服务器 1. 并发问题: 1)丢失更新(同时编辑文件,相继保存,最终丢失先保存者更新的内容) 2)不...
摘要:第一个字被称为。经量级锁的加锁过程当一个对象被锁定时,被复制到当前尝试获取锁的线程的线程栈的锁记录空间被复制的官方称为。根据锁对象目前是否处于被锁定状态,撤销偏向后恢复到未锁定或经量级锁定状态。 Synchronized关键字 synchronized的锁机制的主要优势是Java语言内置的锁机制,因此,JVM可以自由的优化而不影响已存在的代码。 任何对象都拥有对象头这一数据结构来支持锁...
摘要:并发模块本身有两种不同的类型进程和线程,两个基本的执行单元。调用以启动新线程。在大多数系统中,时间片发生不可预知的和非确定性的,这意味着线程可能随时暂停或恢复。 大纲 什么是并发编程?进程,线程和时间片交织和竞争条件线程安全 策略1:监禁 策略2:不可变性 策略3:使用线程安全数据类型 策略4:锁定和同步 如何做安全论证总结 什么是并发编程? 并发并发性:多个计算同时发生。 在现代...
阅读 1918·2021-11-19 09:40
阅读 3071·2021-09-24 10:27
阅读 3383·2021-09-02 15:15
阅读 2016·2019-08-30 15:54
阅读 1357·2019-08-30 15:54
阅读 1495·2019-08-30 13:12
阅读 770·2019-08-28 18:05
阅读 2949·2019-08-27 10:53