This is my Config File(Test.txt)
CommandA 75%
CommandB 15%
CommandC 10%
I wrote a multithreading program in which I am reading the files line by line but not sure how should I do the above question in which this much percentage(75%) of random calls go to CommandA, and this much percentage(15%) of random calls go to CommandB and same with CommandC.
public static void main(String[] args) {
for (int i = 1; i <= threadSize; i++) {
new Thread(new ThreadTask(i)).start();
}
}
class ThreadTask implements Runnable {
public synchronized void run() {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("C:\\Test.txt"));
while ((line = br.readLine()) != null) {
String[] s = line.split("\\s+");
for (String split : s) {
System.out.println(split);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Get a random number 1-100. If the number is 1-75 do command A, 76-90 do command B, 91-100 do command C.
EDITED for comment:
There are two ways I would consider doing this. If you only have the three commands (A, B, C) then you can do a simple:
if you have a lot of complicated commands you can set up the commands like so:
and then choose the command like so: