Java__single process of file lock


import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class OnlyYou
{
// 返回Ture说明已经启动了
public static boolean checkOnly(String name)
{
RandomAccessFile raf = null;
FileChannel channel = null;
FileLock lock = null;

try
{
File file = new File(System.getProperty("java.io.tmpdir") + name + ".only");
file.deleteOnExit();
file.createNewFile();

raf = new RandomAccessFile(file, "rw");
channel = raf.getChannel();
lock = channel.tryLock();
}
catch (Exception e)
{
e.printStackTrace();
}

if (lock == null) return true;
else return false;
}
}

你可能感兴趣的:(Java,myCodeTools)