the LinkedBlockingQueue is much faster than the ArrayBlockingQueue

(strange but this is the result of a test:
ArrayBlockingQueue: 39461 lines / second;
LinkedBlockingQueue: 60774 lines / second)
pull/1/head
Michael Peter Christen 10 years ago
parent 6390454652
commit 783cf6fbc7

@ -53,7 +53,7 @@ public class SynonymLibrary {
File ff = new File(path, f); File ff = new File(path, f);
String line; String line;
try { try {
BlockingQueue<String> list = Files.concurentLineReader(ff, 1000); BlockingQueue<String> list = Files.concurentLineReader(ff);
while ((line = list.take()) != Files.POISON_LINE) { while ((line = list.take()) != Files.POISON_LINE) {
line = line.trim(); line = line.trim();
if (line.length() == 0 || line.charAt(0) == '#') continue; if (line.length() == 0 || line.charAt(0) == '#') continue;

@ -207,7 +207,7 @@ public class Tagging {
ConcurrentLog.info("Tagging", "Started Vocabulary Initialization for " + this.propFile); ConcurrentLog.info("Tagging", "Started Vocabulary Initialization for " + this.propFile);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
long count = 0; long count = 0;
BlockingQueue<String> list = Files.concurentLineReader(this.propFile, 1000); BlockingQueue<String> list = Files.concurentLineReader(this.propFile);
String term, v; String term, v;
String[] tags; String[] tags;
int p; int p;
@ -268,7 +268,8 @@ public class Tagging {
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
} }
ConcurrentLog.info("Tagging", "Finished Vocabulary Initialization for " + this.propFile + "; " + count + " lines; " + (System.currentTimeMillis() - start) + " milliseconds"); long time = Math.max(1, System.currentTimeMillis() - start);
ConcurrentLog.info("Tagging", "Finished Vocabulary Initialization for " + this.propFile + "; " + count + " lines; " + time + " milliseconds; " + (1000L * count / time) + " lines / second");
} }
public boolean isFacet() { public boolean isFacet() {
@ -292,7 +293,7 @@ public class Tagging {
if (this.propFile == null) return; if (this.propFile == null) return;
File tmp = tmpFile(); File tmp = tmpFile();
BufferedWriter w = new BufferedWriter(new FileWriter(tmp)); BufferedWriter w = new BufferedWriter(new FileWriter(tmp));
BlockingQueue<String> list = Files.concurentLineReader(this.propFile, 1000); BlockingQueue<String> list = Files.concurentLineReader(this.propFile);
if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n"); if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n");
if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n"); if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n");
String line; String line;
@ -325,7 +326,7 @@ public class Tagging {
if (this.propFile == null) return; if (this.propFile == null) return;
File tmp = tmpFile(); File tmp = tmpFile();
BufferedWriter w = new BufferedWriter(new FileWriter(tmp)); BufferedWriter w = new BufferedWriter(new FileWriter(tmp));
BlockingQueue<String> list = Files.concurentLineReader(this.propFile, 1000); BlockingQueue<String> list = Files.concurentLineReader(this.propFile);
if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n"); if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n");
if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n"); if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n");
String line; String line;
@ -366,7 +367,7 @@ public class Tagging {
this.objectspace = os; this.objectspace = os;
File tmp = tmpFile(); File tmp = tmpFile();
BufferedWriter w = new BufferedWriter(new FileWriter(tmp)); BufferedWriter w = new BufferedWriter(new FileWriter(tmp));
BlockingQueue<String> list = Files.concurentLineReader(this.propFile, 1000); BlockingQueue<String> list = Files.concurentLineReader(this.propFile);
if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n"); if (this.namespace != null && !this.namespace.equals(DEFAULT_NAMESPACE)) w.write("#namespace:" + this.namespace + "\n");
if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n"); if (this.objectspace != null && this.objectspace.length() > 0) w.write("#objectspace:" + this.objectspace + "\n");
String line; String line;
@ -429,7 +430,7 @@ public class Tagging {
Map<String, SOTuple> map = new LinkedHashMap<String, SOTuple>(); Map<String, SOTuple> map = new LinkedHashMap<String, SOTuple>();
BlockingQueue<String> list; BlockingQueue<String> list;
try { try {
list=Files.concurentLineReader(this.propFile, 1000); list=Files.concurentLineReader(this.propFile);
} catch (final IOException e1) { } catch (final IOException e1) {
return map; return map;
} }

@ -36,6 +36,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
@ -75,8 +76,8 @@ public class Files {
* @throws IOException * @throws IOException
*/ */
public final static String POISON_LINE = "__@POISON__"; public final static String POISON_LINE = "__@POISON__";
public static BlockingQueue<String> concurentLineReader(final File f, final int maxQueueSize) throws IOException { public static BlockingQueue<String> concurentLineReader(final File f) throws IOException {
final BlockingQueue<String> q = new ArrayBlockingQueue<String>(maxQueueSize); final BlockingQueue<String> q = new LinkedBlockingQueue<String>();
final InputStream is = read(f); final InputStream is = read(f);
final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
Thread t = new Thread() { Thread t = new Thread() {

@ -49,7 +49,7 @@ public class URLRewriterLibrary {
for (final String f: files) { for (final String f: files) {
File ff = new File(this.rewritingPath, f); File ff = new File(this.rewritingPath, f);
try { try {
BlockingQueue<String> list = Files.concurentLineReader(ff, 1000); BlockingQueue<String> list = Files.concurentLineReader(ff);
String line; String line;
while ((line = list.take()) != Files.POISON_LINE) { while ((line = list.take()) != Files.POISON_LINE) {
line = line.trim(); line = line.trim();

Loading…
Cancel
Save