java nio 添加锁_NIO读写文件并加锁

一、读取文件

1 packagelock;2

3 importjava.io.File;4 importjava.io.FileNotFoundException;5 importjava.io.IOException;6 importjava.io.RandomAccessFile;7 importjava.nio.channels.FileChannel;8 importjava.nio.channels.FileLock;9 importjava.util.Calendar;10

11 public class ReadFileLock implementsRunnable {12

13 public voidrun() {14 try{15 Calendar calstart =Calendar.getInstance();16 Thread.sleep(5000);17 File file = new File("D:/test.txt");18 //给该文件加锁

19 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");20 FileChannel fileChannel =randomAccessFile.getChannel();21 FileLock fileLock = null;22 while (true) {23 try{24 fileLock =fileChannel.tryLock();25 break;26 } catch(Exception e) {27 System.out.println("有其他线程正在操作该文件,当前线程" + Thread.currentThread().getName() + "休眠1000毫秒");28 Thread.sleep(1000);29 }30 }31 byte[] buf = new byte[1024];32 StringBuffer sb = newStringBuffer();33 while ((randomAccessFile.read(buf)) != -1) {34 sb.append(new String(buf, "utf-8"));35 buf = new byte[850];36 }37

38 System.err.println(sb.toString());39

40 fileLock.release();41 fileChannel.close();42 randomAccessFile.close();43 randomAccessFile = null;44

45 Calendar calend =Calendar.getInstance();46 System.out.println("当前线程:" + Thread.currentThread().getName() + ",读文件共花了"

47 + (calend.getTimeInMillis() - calstart.getTimeInMillis()) + "秒");48 } catch(FileNotFoundException e) {49 e.printStackTrace();50 } catch(IOException e) {51 e.printStackTrace();52 } catch(InterruptedException e) {53 e.printStackTrace();54 }55 }56

57 public static voidmain(String[] args) {58 ReadFileLock threadTarget = newReadFileLock();59 Thread read = newThread(threadTarget);60 read.setName("thread-read-file");61 read.start();62 Thread read2 = newThread(threadTarget);63 read2.setName("thread-write-file2");64 read2.start();65 }66

67 }

二、写文件

package lock;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.channels.FileChannel;

import java.nio.channels.FileLock;

import java.util.Calendar;

public class ReadFileLock implements Runnable {

public void run() {

try {

Calendar calstart = Calendar.getInstance();

Thread.sleep(5000);

File file = new File("D:/test.txt");

// 给该文件加锁

RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

FileChannel fileChannel = randomAccessFile.getChannel();

FileLock fileLock = null;

while (true) {

try {

fileLock = fileChannel.tryLock();

break;

} catch (Exception e) {

System.out.println("有其他线程正在操作该文件,当前线程" + Thread.currentThread().getName() + "休眠1000毫秒");

Thread.sleep(1000);

}

}

byte[] buf = new byte[1024];

StringBuffer sb = new StringBuffer();

while ((randomAccessFile.read(buf)) != -1) {

sb.append(new String(buf, "utf-8"));

buf = new byte[850];

}

System.err.println(sb.toString());

fileLock.release();

fileChannel.close();

randomAccessFile.close();

randomAccessFile = null;

Calendar calend = Calendar.getInstance();

System.out.println("当前线程:" + Thread.currentThread().getName() + ",读文件共花了"

+ (calend.getTimeInMillis() - calstart.getTimeInMillis()) + "秒");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

ReadFileLock threadTarget = new ReadFileLock();

Thread read = new Thread(threadTarget);

read.setName("thread-read-file");

read.start();

Thread read2 = new Thread(threadTarget);

read2.setName("thread-write-file2");

read2.start();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值