文件锁

在某些应用场景下,需要对特定资源执行顺序处理,使用状态标记处理比较繁琐.因此可以引入文件锁.

范例如下:

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.Date;


public class FileLockTest extends Thread {


private String fileLock;
private String fileName;


private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public FileLockTest(String threadName, String fileName) {


super.setName(threadName);
this.fileName = fileName;
this.fileLock = fileName + ".lock";
}


public void run() {


boolean finish = false;
do {
try {
FileOutputStream fis = new FileOutputStream(fileLock);
FileChannel fc = fis.getChannel();
FileLock flock = fc.tryLock();
if (flock.isValid()) {
System.out.println(sdf.format(new Date()) + this.getName() + fileName + "is locked");


try {  

/*PrintWriter pw = new PrintWriter(new FileWriter(fileName,true));  
            pw.println(sdf.format(new Date()) + this.getName() + "休眠5000毫秒"  ); 
            
            this.sleep(5000);
            
            pw.println(sdf.format(new Date()) + this.getName() + "休眠5000毫秒完成" );  
            pw.close();  */

System.out.println(sdf.format(new Date()) + this.getName() + "休眠5000毫秒"  ); 
this.sleep(5000);
System.out.println(sdf.format(new Date()) + this.getName() + "休眠5000毫秒完成" );  
            
            try {
flock.release();
fc.close();
fis.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
            
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  

finish = true;



} else {
System.out.println(sdf.format(new Date()) + this.getName() + fileName + "is locked fail");
finish = false;
try {
this.sleep(1000);
System.out.println(sdf.format(new Date()) + this.getName() + "休眠1000毫秒后重试!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}catch(Exception ex) {
//ex.printStackTrace();
try {
this.sleep(1000);
System.out.println(sdf.format(new Date()) + this.getName() +"加锁失败" + "休眠1000毫秒后重试!" + ex.getMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


} while (!finish);
}



public static void main(String[] args) {

for(int i= 0 ;i<5; i++) {
FileLockTest flt = new FileLockTest("thread" + i , "d:/flt.txt");
flt.start();
}

}

}

输出如下:

2018-05-31 02:34:49thread1d:/flt.txtis locked
2018-05-31 02:34:49thread1休眠5000毫秒
2018-05-31 02:34:50thread0加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:50thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:50thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:50thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:51thread0加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:51thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:51thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:51thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:52thread0加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:52thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:52thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:52thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:53thread0加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:53thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:53thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:53thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:54thread1休眠5000毫秒完成
2018-05-31 02:34:54thread0加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:54thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:54thread0d:/flt.txtis locked
2018-05-31 02:34:54thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:54thread0休眠5000毫秒
2018-05-31 02:34:54thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:55thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:55thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:55thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:56thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:56thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:56thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:57thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:57thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:57thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:58thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:58thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:58thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:59thread0休眠5000毫秒完成
2018-05-31 02:34:59thread3加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:59thread3d:/flt.txtis locked
2018-05-31 02:34:59thread3休眠5000毫秒
2018-05-31 02:34:59thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:34:59thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:00thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:00thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:01thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:01thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:02thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:02thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:03thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:03thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:04thread3休眠5000毫秒完成
2018-05-31 02:35:04thread4加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:04thread4d:/flt.txtis locked
2018-05-31 02:35:04thread4休眠5000毫秒
2018-05-31 02:35:04thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:05thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:06thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:07thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:08thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:09thread4休眠5000毫秒完成
2018-05-31 02:35:09thread2加锁失败休眠1000毫秒后重试!null
2018-05-31 02:35:09thread2d:/flt.txtis locked
2018-05-31 02:35:09thread2休眠5000毫秒
2018-05-31 02:35:14thread2休眠5000毫秒完成

你可能感兴趣的:(文件锁)