日期:2014-05-16  浏览次数:21134 次

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 读写文件时会阻塞,对文件加锁神马的的,待研究.
 * 
 * @author 10106511
 *
 */
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.p