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