Skip to content

Instantly share code, notes, and snippets.

@RodionGork
Created February 24, 2014 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RodionGork/9184975 to your computer and use it in GitHub Desktop.
Save RodionGork/9184975 to your computer and use it in GitHub Desktop.
public void generate(String path) {
System.out.println("Creating " + cores + " threads...");
ExecutorService service = Executors.newFixedThreadPool(cores);
prepareWords(false);
for (int i = 0; i < numFiles; i++) {
String name = String.format("%s/%08d.txt", path, i);
service.execute(new FileGenerator(name));
}
service.shutdown();
try {
service.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private class FileGenerator implements Runnable {
private String name;
public FileGenerator(String name) {
this.name = name;
}
public void run() {
try (OutputStream output = new BufferedOutputStream(new FileOutputStream(name))) {
generateFile(output);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void generateFile(OutputStream output) throws IOException {
int size = rnd.nextInt(maxSize - minSize + 1) + minSize;
int cnt = 0;
while (size > 0) {
pos = (pos + rnd.nextInt(5) + 1) % words.length;
byte[] w = words[pos];
output.write(w, 0, w.length);
cnt += w.length + 1;
if (cnt < lineLength) {
output.write(' ');
} else {
output.write('\n');
size -= cnt;
cnt = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment