NIO讀寫文件並加鎖
阿新 • • 發佈:2018-04-19
lec inter string trace acc 加鎖 target ioe stringbu
一、讀取文件
1 package lock; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.RandomAccessFile; 7 import java.nio.channels.FileChannel; 8 import java.nio.channels.FileLock; 9 import java.util.Calendar; 10 11 public class ReadFileLock implementsRunnable { 12 13 public void run() { 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 = new StringBuffer(); 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 void main(String[] args) { 58 ReadFileLock threadTarget = new ReadFileLock(); 59 Thread read = new Thread(threadTarget); 60 read.setName("thread-read-file"); 61 read.start(); 62 Thread read2 = new Thread(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(); } }
NIO讀寫文件並加鎖