Timer replaced with JobScheduler to reduce extra threads created

This commit is contained in:
Alexey Kudravtsev
2016-03-14 14:45:59 +03:00
parent 710cfa9f36
commit b376b6a53b
2 changed files with 25 additions and 28 deletions
@@ -15,6 +15,7 @@
*/
package git4idea.commands;
import com.intellij.concurrency.JobScheduler;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
@@ -32,8 +33,8 @@ import git4idea.GitVcs;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@@ -258,7 +259,7 @@ public class GitTask {
// To minimize code duplication we use GitTaskDelegate.
private abstract class BackgroundableTask extends Task.Backgroundable implements TaskExecution {
private GitTaskDelegate myDelegate;
private final GitTaskDelegate myDelegate;
public BackgroundableTask(@Nullable final Project project, @NotNull GitHandler handler, @NotNull final String processTitle) {
super(project, processTitle, true);
@@ -308,7 +309,7 @@ public class GitTask {
}
private abstract class ModalTask extends Task.Modal implements TaskExecution {
private GitTaskDelegate myDelegate;
private final GitTaskDelegate myDelegate;
public ModalTask(@Nullable final Project project, @NotNull GitHandler handler, @NotNull final String processTitle) {
super(project, processTitle, true);
@@ -338,11 +339,11 @@ public class GitTask {
* If yes, kills the GitHandler.
*/
private static class GitTaskDelegate implements Disposable {
private GitHandler myHandler;
private final GitHandler myHandler;
private ProgressIndicator myIndicator;
private TaskExecution myTask;
private Timer myTimer;
private Project myProject;
private final TaskExecution myTask;
private ScheduledFuture<?> myTimer;
private final Project myProject;
public GitTaskDelegate(Project project, GitHandler handler, TaskExecution task) {
myProject = project;
@@ -353,27 +354,26 @@ public class GitTask {
public void run(ProgressIndicator indicator) {
myIndicator = indicator;
myTimer = new Timer("Git task delegate");
myTimer.schedule(new TimerTask() {
@Override
public void run() {
myTimer = JobScheduler.getScheduler().scheduleWithFixedDelay(
()-> {
if (myIndicator != null && myIndicator.isCanceled()) {
try {
if (myHandler != null) {
myHandler.destroyProcess();
}
} finally {
Disposer.dispose(GitTaskDelegate.this);
}
finally {
Disposer.dispose(this);
}
}
}
}, 0, 200);
}, 0, 200, TimeUnit.MILLISECONDS);
myTask.execute(indicator);
}
@Override
public void dispose() {
if (myTimer != null) {
myTimer.cancel();
myTimer.cancel(false);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package org.jetbrains.idea.svn.auth;
import com.intellij.concurrency.JobScheduler;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
@@ -65,7 +66,8 @@ import java.io.FilenameFilter;
import java.net.*;
import java.util.*;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class SvnAuthenticationNotifier extends GenericNotifierImpl<SvnAuthenticationNotifier.AuthenticationRequest, SVNURL> {
private static final Logger LOG = Logger.getInstance(SvnAuthenticationNotifier.class);
@@ -76,7 +78,7 @@ public class SvnAuthenticationNotifier extends GenericNotifierImpl<SvnAuthentica
private final SvnVcs myVcs;
private final RootsToWorkingCopies myRootsToWorkingCopies;
private final Map<SVNURL, Boolean> myCopiesPassiveResults;
private Timer myTimer;
private ScheduledFuture<?> myTimer;
private volatile boolean myVerificationInProgress;
public SvnAuthenticationNotifier(final SvnVcs svnVcs) {
@@ -91,18 +93,13 @@ public class SvnAuthenticationNotifier extends GenericNotifierImpl<SvnAuthentica
if (myTimer != null) {
stop();
}
myTimer = new Timer("SVN authentication timer");
myTimer =
// every 10 minutes
myTimer.schedule(new TimerTask() {
@Override
public void run() {
myCopiesPassiveResults.clear();
}
}, 10000, 10 * 60 * 1000);
JobScheduler.getScheduler().scheduleWithFixedDelay(myCopiesPassiveResults::clear, 10, 10 * 60, TimeUnit.SECONDS);
}
public void stop() {
myTimer.cancel();
myTimer.cancel(false);
myTimer = null;
}