Reputation: 14616
My application generates a lot of log files, I want to clean this folder each time I startup the application. The simplest way to do that is:
//remove files from the previous executions
Thread tCleanAppFolder = new Thread() {
public void run() {
String savePath = getFilesDir().getPath() + File.separator;
File path = new File(savePath);
for (File file : path.listFiles()) {
file.delete();
}
}
};
tCleanAppFolder.start();
Of course, we execute this code in the separate thread for the performance reasons.
My questions:
Should I remove each file in the separate thread or it's OK to leave the code as it's now?
Will the usage of the multiple threads inside of tCleanAppFolder
thread boost the cleaning process?
Probably, it's worth to mention, I'm talking about Android application thus, there is no HDD, but internal and external memory of a smartphone.
Upvotes: 0
Views: 2313
Reputation: 19821
If you are simply deleting files (cpu-wise the time needed is negligible and individually it is the same from the i/o point of view too) the bottleneck will be represented by disk I/O, i don't expect to see any improvement deleting file with multiple threads, you can just do it sequentially.
Upvotes: 1
Reputation: 409
If you are trying to start the log file afresh instaed of appending the current log you can use
<appender name="RFA" class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="false" />
I use this during my development This does not delete or clean if the log file is rolled.
Upvotes: 0
Reputation: 7700
Don't prematurely optimize until you know for sure that this code is a bottleneck. The time can always be spent fixing another bug or adding another feature.
Making it multi-threaded will make your application state more complex and thus harder to debug and the source code harder to read. So before you do this, I would run benchmarks to actually determine whether or not this operation is thread-bound and could actually be optimized by using more threads.
Upvotes: 2
Reputation: 1657
are you sure you are not constructing any new files before
your 'file deleting' thread executes this path.listFiles()
?
if not you might delete files that are currently needed !
apart from that spinning of some more threads would probably improve performance since in each iteration of the loop you will have a lot of IO-Time. In other words ... your thread is not CPU-bound but IO-bound, so spinning of more threads than you have CPU cores might pay off.
on the other hand you should not start thousands of threads if it might happen that you have thousands of files. in that case your logic would be a bit more complex to distribute the files to delete over the threads.
Upvotes: 1