I have program that uses 4 input .txt files. When program is started, certain data manipulation is done, data is grabbed from those 4 files, and program creates 2 output .txt files at the end. Program execution time at that time is about 18 seconds. I would like to know what would be fastest way to write this programs in terms of program execution? This is how program is written now:
First, I started to read and write data sequentially, so my program logic is based on 2 parts - One loop (for reading necessary data and file creation) is used for first output file, and another loop (also for reading necessary data and file creation for that fole) is used for second output file. Second method needed to wait to first method to be done, with this approach. My total time of execution was at this point about 18 seconds.
Then, I involved theads - I used 2 threadsm (one thread running it's loop) so every loop would be running in separate thread. With that approach, I cut total time of execution to about 9 seconds.
Now; I started to ask If I could be speeding up total time of execution even more?
My code for the first thread looks like this (and the code for other thread is basically more or less similar):
Thread thread1 = new Thread() {
public void run() {
final List<Articles> article_list = ac.getFileValues("resources/articles.txt");
String file_contents = "";
String file1_data = "";
for (int i = 0; i < article_list.size(); i++) {
double price_art_local_val = cc.getPrice("resources/pricelist.txt", article_list.get(i).sifra);
double[] art_all_shops = sc.getAmountInAllStores("resources/supply.txt", article_list.get(i).sifra);
double total_value_art_all_shops_local = price_art_local_val * art_all_shops[0];
double total_value_art_all_shops_foregin = total_value_art_all_shops_local / exchange_rate;
file_contents = article_list.get(i).sifra+"\t"+article_list.get(i).naziv+"\t"+df.format(price_art_local_val)+"\t"+df.format(art_all_shops[0])+"\t"+article_list.get(i).jedinica_mjere+"\t"+df.format(total_value_art_all_shops_local)+"\t"+df.format(total_value_art_all_shops_foregin)+"\t"+df.format(art_all_shops[1])+"\n";
System.out.print(file_contents);
file1_data += file_contents;
}
if(file1_data != "")
{
save.saveFile("results/supply_value_articles.txt", file1_data);
}
}
};
The place I see further execution time reduction in my opinion is on methods cc.getPrice and sc.getAmountInAllStores() in that piece of code. My view is that It would be good to achieve that they run in separate threads, so other method would not have to wait execution of first method. Am I on good track?
So I presume that if I want to speed up execution in for loop I would need to make execution of methods cc.getPrice() and sc.getAmountInAllStores() also in separate threads. If that is not right solution, I would like to know what to do.
Then, if this is right solution, how can I achieve that? I do not know how to properly write code to use another thread, if that code is already in run() method. Can it be done even, I am not sure? Also, that methods return certain values, and they need to be stored in variables. So I would need also to get data for that metods stored; meaning threads would return data for me. I do not know how to done that properly. Seems like this won't help me then to basically write this (if I create one thread for method cc.getPrice, and another thread for method sc.getAmountInAllStores and name them thread3 and thread4):
thread3.join();
thread4.join();
I would need piece of code showing the appropriate solution. If I am not on the right track, and this can't be done (starting and using new threads inside running thread), please instruct me what to do. I have read some stackowerflow questions about BlockingQuene, but I think my approach brings something else that I need.
Please help with examples of code If you can. Thank you very much, help appreciated.