• NIO读写文件并加锁


    一、读取文件

     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 implements Runnable {
    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();
    	}
    
    } 
    

      

  • 相关阅读:
    [译]Mongoose指南
    shell去掉后缀方法
    memcache和memcached区别
    ssh隧道远程连接mysql
    nginx实现某个页面http访问,其余全部跳转到https
    es快照和备份
    elasticsearch 单节点实现
    logstash5安装并实现mariadb数据写入到elasticsearch
    linux 内存分析
    zabbix api创建screen vsize限制解决
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/8883230.html
Copyright © 2020-2023  润新知