-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkQueue.java
36 lines (29 loc) · 940 Bytes
/
LinkQueue.java
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
package scraping;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.stream.Stream;
public class LinkQueue {
private ArrayList<String> links = new ArrayList<>();
public synchronized void put(String link) {
links.add(link);
notifyAll();
}
public synchronized String get() throws InterruptedException {
while(links.size() == 0) wait();
String result = links.get(0);
links.remove(0);
notifyAll();
return result;
}
public synchronized void loadFromFile(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(file));
Stream<String> stringStream = reader.lines();
stringStream.forEach(line -> { links.add(line); });
reader.close();
}
public synchronized int size() { return links.size(); }
}