本文整理了Java中java.nio.channels.FileChannel.tryLock()
方法的一些代码示例,展示了FileChannel.tryLock()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileChannel.tryLock()
方法的具体详情如下:
包路径:java.nio.channels.FileChannel
类名称:FileChannel
方法名:tryLock
[英]Attempts to acquire an exclusive lock on this file without blocking.
This is a convenience method for attempting to acquire a maximum length lock on the file. It is equivalent to: fileChannel.tryLock(0L, Long.MAX_VALUE, false);
The method returns null if the acquisition would result in an overlapped lock with another OS process.
[中]尝试在不阻塞的情况下获取此文件的独占锁。
这是尝试获取文件最大长度锁的一种方便方法。它相当于:fileChannel。tryLock(0升,长最大值,假);
如果采集将导致与另一个OS进程的重叠锁定,则该方法返回null。
代码示例来源:origin: com.h2database/h2
@Override
public synchronized FileLock tryLock(long position, long size,
boolean shared) throws IOException {
return file.getChannel().tryLock(position, size, shared);
}
代码示例来源:origin: SonarSource/sonarqube
public boolean tryLock() {
try {
lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
lockChannel = lockRandomAccessFile.getChannel();
lockFile = lockChannel.tryLock(0, 1024, false);
return lockFile != null;
} catch (IOException e) {
throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
}
}
代码示例来源:origin: stackoverflow.com
file = new RandomAccessFile("file.txt", "rw");
FileChannel channel = file.getChannel();
fl = channel.tryLock();
// file now is locked
fl.release();
代码示例来源:origin: graphhopper/graphhopper
public static void main(String[] args) throws IOException {
// trying FileLock mechanics in different processes
File file = new File("tmp.lock");
file.createNewFile();
FileChannel channel = new RandomAccessFile(file, "r").getChannel();
boolean shared = true;
FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
System.out.println("locked " + lock1);
System.in.read();
System.out.println("release " + lock1);
lock1.release();
}
代码示例来源:origin: com.h2database/h2
@Override
public synchronized FileLock tryLock(long position, long size,
boolean shared) throws IOException {
return file.getChannel().tryLock(position, size, shared);
}
代码示例来源:origin: apache/incubator-dubbo
lockfile.createNewFile();
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
FileChannel channel = raf.getChannel()) {
FileLock lock = channel.tryLock();
if (lock == null) {
throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties");
代码示例来源:origin: lealone/Lealone
@Override
public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException {
return file.getChannel().tryLock(position, size, shared);
}
代码示例来源:origin: apache/incubator-dubbo
lockfile.createNewFile();
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
FileChannel channel = raf.getChannel()) {
FileLock lock = channel.tryLock();
if (lock == null) {
throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties");
代码示例来源:origin: lealone/Lealone
@Override
public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException {
return file.getChannel().tryLock(position, size, shared);
}
代码示例来源:origin: apache/incubator-dubbo
lockfile.createNewFile();
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
FileChannel channel = raf.getChannel()) {
FileLock lock = channel.tryLock();
if (lock == null) {
throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties");
代码示例来源:origin: ltsopensource/light-task-scheduler
/**
* 获得锁
*/
public boolean tryLock() {
boolean success = false;
try {
if (channel != null && channel.isOpen()) {
return false;
}
channel = randomAccessFile.getChannel();
lock = channel.tryLock();
if (lock != null) {
success = true;
return true;
}
} catch (Exception e) {
return false;
} finally {
if (!success) {
if (channel != null) {
try {
channel.close();
} catch (IOException ignored) {
}
}
}
}
return false;
}
代码示例来源:origin: apache/incubator-dubbo
lockfile.createNewFile();
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
FileChannel channel = raf.getChannel()) {
FileLock lock = channel.tryLock();
if (lock == null) {
throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties");
代码示例来源:origin: ltsopensource/light-task-scheduler
/**
* 获得锁
*/
public boolean tryLock() {
boolean success = false;
try {
if (channel != null && channel.isOpen()) {
return false;
}
channel = randomAccessFile.getChannel();
lock = channel.tryLock();
if (lock != null) {
success = true;
return true;
}
} catch (Exception e) {
return false;
} finally {
if (!success) {
if (channel != null) {
try {
channel.close();
} catch (IOException ignored) {
}
}
}
}
return false;
}
代码示例来源:origin: javamelody/javamelody
private FileLock getFileLock() {
if (fileLock == null) {
try {
final File storageDir = lockFile.getParentFile();
if (!storageDir.mkdirs() && !storageDir.exists()) {
return null;
}
if (input == null || fileChannel == null) {
input = new RandomAccessFile(lockFile, "rw");
fileChannel = input.getChannel();
}
fileLock = fileChannel.tryLock();
} catch (final IOException e) {
return null;
} catch (final OverlappingFileLockException e) {
return null;
}
}
return fileLock;
}
代码示例来源:origin: apache/rocketmq
/**
* @throws Exception
*/
public void start() throws Exception {
lock = lockFile.getChannel().tryLock(0, 1, false);
if (lock == null || lock.isShared() || !lock.isValid()) {
throw new RuntimeException("Lock failed,MQ already started");
}
lockFile.getChannel().write(ByteBuffer.wrap("lock".getBytes()));
lockFile.getChannel().force(true);
this.flushConsumeQueueService.start();
this.commitLog.start();
this.storeStatsService.start();
if (this.scheduleMessageService != null && SLAVE != messageStoreConfig.getBrokerRole()) {
this.scheduleMessageService.start();
}
if (this.getMessageStoreConfig().isDuplicationEnable()) {
this.reputMessageService.setReputFromOffset(this.commitLog.getConfirmOffset());
} else {
this.reputMessageService.setReputFromOffset(this.commitLog.getMaxOffset());
}
this.reputMessageService.start();
this.haService.start();
this.createTempFile();
this.addScheduleTask();
this.shutdown = false;
}
代码示例来源:origin: apache/flume
/**
* Attempts to acquire an exclusive lock on the directory.
*
* @return A lock object representing the newly-acquired lock or
* <code>null</code> if directory is already locked.
* @throws IOException if locking fails.
*/
@SuppressWarnings("resource")
private FileLock tryLock(File dir) throws IOException {
File lockF = new File(dir, FILE_LOCK);
lockF.deleteOnExit();
RandomAccessFile file = new RandomAccessFile(lockF, "rws");
FileLock res = null;
try {
res = file.getChannel().tryLock();
} catch (OverlappingFileLockException oe) {
file.close();
return null;
} catch (IOException e) {
LOGGER.error("Cannot create lock on " + lockF, e);
file.close();
throw e;
}
return res;
}
代码示例来源:origin: apache/ignite
/**
* Performs a lock (shared or exclusive) on a file, that
* this lock instance was constructed with.
*
* @param shared Whether a lock is shared (non-exclusive).
* @throws IgniteCheckedException If failed to perform locking. The file remains open.
*/
public void lock(boolean shared) throws IgniteCheckedException {
if (fileLock != null)
throw new IgniteCheckedException("Already locked [lockFile=" + file + ']');
try {
fileLock = raFile.getChannel().tryLock(0, Long.MAX_VALUE, shared);
if (fileLock == null)
throw new IgniteCheckedException("Failed to get exclusive lock on lock file [lockFile=" + file + ']');
}
catch (IOException | OverlappingFileLockException e) {
throw new IgniteCheckedException("Failed to get exclusive lock on lock file [lockFile=" + file + ']', e);
}
}
代码示例来源:origin: stackoverflow.com
private static boolean lockInstance(final String lockFile) {
try {
final File file = new File(lockFile);
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
if (fileLock != null) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
fileLock.release();
randomAccessFile.close();
file.delete();
} catch (Exception e) {
log.error("Unable to remove lock file: " + lockFile, e);
}
}
});
return true;
}
} catch (Exception e) {
log.error("Unable to create and/or lock file: " + lockFile, e);
}
return false;
}
代码示例来源:origin: apache/ignite
assert lockFile != null;
FileChannel ch = lockFile.getChannel();
lock = ch.tryLock(0, 1, false);
代码示例来源:origin: stackoverflow.com
try {
// Get a file channel for the file
File file = new File("filename");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Use the file channel to create a lock on the file.
// This method blocks until it can retrieve the lock.
FileLock lock = channel.lock();
/*
use channel.lock OR channel.tryLock();
*/
// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
// File is already locked in this thread or virtual machine
}
// Release the lock - if it is not null!
if( lock != null ) {
lock.release();
}
// Close the file
channel.close();
} catch (Exception e) {
}
内容来源于网络,如有侵权,请联系作者删除!