-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataReader.java
More file actions
53 lines (47 loc) · 1.77 KB
/
Copy pathDataReader.java
File metadata and controls
53 lines (47 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class DataReader implements Runnable {
private String path; // 文件路径
private BlockingQueue<DataRow> queue; // 内存缓冲区
//private static final int SLEEPTIME = 1000;
public DataReader(BlockingQueue<DataRow> queue, String path) {
this.queue = queue;
this.path = path;
}
@Override
public void run() {
File file = new File(path);
DataRow row = null;
//Random r = new Random();
try {
//System.out.println("Start reading file: thread " + Thread.currentThread().getId());
if (file.exists()) {
BufferedReader br = new BufferedReader(new FileReader(file));
String temp;
int i = 0;
while ((temp = br.readLine()) != null) {
String[] line = temp.split(",");
if (line.length != 2) {
System.out.println("error!");
return;
}
//Thread.sleep(r.nextInt(SLEEPTIME));
row = new DataRow(line[0], line[1]);
//System.out.println(row + " 加入队列");
if (!queue.offer(row, 5, TimeUnit.SECONDS)) {
System.err.println(" 加入队列失败");
}
}
}
//System.out.println("Finish reading file: thread " + Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}