I have to write in a file all possible combinations resulting from the lottery using threads. Example:
- 0 0 0 0 0 0 (first combination)
- 0 0 0 0 0 1 (Second combination)
- 0 0 0 0 0 2
. . .
Last. 38 38 38 38 38 38 (Last combination)
In my main class i just use one thread because i dont know how i can use more threads for write in the file faster.
For generates the numbers i uses 6 loops one inside the other this way:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Proceso extends Thread {
File file = new File("lottery.txt");
public Proceso(String msg) {
super(msg);
}
public void run() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
StringBuffer linea = new StringBuffer();
for (int i = 0; i < 15;i++) {
for (int j = 0; j < 39; j++) {
for (int j2 =0; j2 < 39; j2++) {
for (int k = 0; k < 39; k++) {
for (int k2 = 0 ; k2 < 39; k2++) {
for (int l = 0; l < 39; l++) {
linea.append(i + " " +j + " " +j2 + " " +k + " " +k2 + " " +l + "\n");
bw.write(linea.toString());
}
}
}
}
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Also the file (in middle of the execution) rise a size of 8gb and i have to stop it because my pc collapse.
Edit: if i cant do that, at least i can write with two different threads in the same file at almost the same time? If i can do that how i would do it?