RandomAccessFile 类读写测试及其性能优化(二)

package io;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * RandomAccessFile 类读写测试及其性能优化(二)
 * 
 * 读取 RandomAccessFile 类读写测试及其性能优化(一) 
 * (链接 http://jackyin5918.iteye.com/blog/2022888 )中
 * GenerateIntArray 生成的并保存到文件的数据.
 * 
 * 多线程读取文件和多线程写文件一样没有提升速度.
 * 
 * 疑似RandomAccessFile 读写文件时会阻塞,对文件加锁神马的的,待研究.
 * 
 *
 */
public class GetTheMiddle
{
  private int     count = 1000; // 数组的个数,
  private int     size  = 10;   // 每个数组的元素个数
  private int[][] dataArr;

  public GetTheMiddle()
  {
    dataArr = new int[count][size];
  }

  public GetTheMiddle(int count, int size)
  {
    this.count = count;
    this.size = size;
    this.dataArr = new int[count][size];
  }

  public int[][] getDataArr()
  {
    return dataArr;
  }

  public int[][] readData(File f)
  {
    try
    {
      RandomAccessFile rf = new RandomAccessFile(f, "r");
      for (int i = 0; i < count; i++)
      {
        for (int j = 0; j < size; j++)
        {
          dataArr[i][j] = rf.readInt();
        }
      }
      rf.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    return dataArr;
  }
  
  class ReadDataTask implements Runnable
  {

    private File f;
    private int dataIndex;
    
    public ReadDataTask(File f,int dataIndex)
    {
      this.f = f;
      this.dataIndex = dataIndex;
    }
    
    @Override
    public void run()
    {
      try
      {
        RandomAccessFile rf = new RandomAccessFile(f, "r");
        rf.skipBytes(dataIndex * size * 4 );
        
        for (int j = 0; j < size; j++)
        {
          dataArr[dataIndex][j] = rf.readInt();
        }
        rf.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    
  }
  
  public int[][] readData_M(File f)
  {
    try
    {
      ExecutorService exec = Executors.newCachedThreadPool();
      for(int i=0;i<count;i++)
      {
        exec.execute(new ReadDataTask(f,i));
      }
      exec.shutdown();
      while(true)
      {
        if(exec.isTerminated()) break;
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    
    return dataArr;
  }

  public static void main(String[] args)
  {
    int count = 3;
    int size = 4;
    boolean bPrintData = true; // 是否打印生成的数组,当数据量大是不打印,只在小数据量时打印以便测试
    System.out.printf("count = %d, size = %d \n\n", count, size);

    GenerateIntArray generator = new GenerateIntArray(count, size);
    GetTheMiddle gm = new GetTheMiddle(count,size);

    File f;
    try
    {
      f = new File("D:\\D\\test_data.dat");
      generator.refreshDataArr();
      generator.writeData2File_B(f);
      
      System.out.println("正在读取数据,请稍后...");
      long startTmie = System.nanoTime();
      gm.readData(f);
      long totalTime = (System.nanoTime() - startTmie)/ 100000;
      System.out.println("readData(f) 读取数据成功, 耗时:" + totalTime);
      System.out.println();
      
      System.out.println("正在读取数据,请稍后...");
      startTmie = System.nanoTime();
      gm.readData_M(f);
      totalTime = (System.nanoTime() - startTmie)/ 100000;
      System.out.println("readData_M(f) 读取数据成功, 耗时:" + totalTime);
      System.out.println();
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    
    if(bPrintData)
    {
      System.out.println("generator中生成的数据...");
      int[][] intArr = generator.getDataArr();
      for (int i = 0; i < count; i++)
      {
        for (int j = 0; j < size; j++)
        {
          System.out.printf("%d ", intArr[i][j]);
        }
        System.out.println();
      }
      
      System.out.println("读取出来的数组...");
      intArr = gm.getDataArr();
      for (int i = 0; i < count; i++)
      {
        for (int j = 0; j < size; j++)
        {
          System.out.printf("%d ", intArr[i][j]);
        }
        System.out.println();
      }
    }
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值