Created
February 24, 2014 10:07
-
-
Save RodionGork/9184975 to your computer and use it in GitHub Desktop.
fragment of code from https://github.com/RodionGork/RandomDocuments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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