Move each FileObserver onto its own thread

This commit is contained in:
Tad 2017-12-27 16:02:37 -05:00
parent abf4baf16d
commit 73be65ec2c
2 changed files with 13 additions and 6 deletions

2
.idea/misc.xml generated
View file

@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View file

@ -11,6 +11,7 @@ import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import gnu.trove.set.hash.THashSet;
@ -29,7 +30,10 @@ public class MalwareScannerService extends Service {
@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
threadPoolExecutor = Utils.getThreadPoolExecutor();
malwareMonitors.clear();
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(Utils.getMaxThreads() + malwareMonitors.size());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
@ -37,11 +41,14 @@ public class MalwareScannerService extends Service {
}
});
malwareMonitors.clear();
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
malwareMonitor.startWatching();
}
});
}
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
setForeground();