锁住程序,只允许一个程序运行 FileLock

http://www.cnblogs.com/DreamDrive/p/7425281.html

Demo

private static void makeSingle(){
  RandomAccessFile raf=null;
FileChannel channel=null;
FileLock lock=null;
try{
File sf=new File("aaaa.single");
sf.deleteOnExit();
sf.createNewFile();
raf=new RandomAccessFile(sf,"rw");
channel=raf.getChannel();
lock=channel.tryLock();


}catch(Exception e){
e.printStackTrace();
}  


}
if(lock==null){
system.exit(0);//正常退出程序
}

  

posted @ 2020-01-17 10:11  哎呀呀呀呀~~  阅读(193)  评论(0编辑  收藏  举报