Class InterruptingProgressMonitor

java.lang.Object
com._1c.g5.v8.dt.common.runtime.InterruptingProgressMonitor

public final class InterruptingProgressMonitor extends Object
Bridges Eclipse IProgressMonitor with JDK Thread.interrupt().

Eclipse assumes that code should synchronously poll IProgressMonitor.isCanceled(). However, this is problematic in the following cases:

  • Eclipse unaware code does not know anything about IProgressMonitor and so cannot poll it.
  • Code in JDK and most 3rd party libraries knows nothing about IProgressMonitor and so cannot poll it.
  • IProgressMonitor must be passed around through every frame of the stack both in depth and width. If original user-controlled IProgressMonitor is lost and later replaced with NullProgressMonitor then cancellation ability is lost.
For the specified cases, this very bridge can be used to notify code based on the classic JDK approach that a user has requested cancellation.

For example, given the following fragment

 InterruptingProgressMonitor.runInterruptibly(monitor, () -> {
  Thread.sleep(10000000);
 });
 
the current thread will fall out of sleep if user cancells job related to the specified monitor.

The implementation starts another daemon thread for the time of execution of the specified code block. If this new daemon thread detects that the specified monitor has been cancelled then it interrupts original thread and ends.

See Also:
  • Method Details

    • runInterruptibly

      public static <T, E1 extends Throwable, E2 extends Throwable> T runInterruptibly(org.eclipse.core.runtime.IProgressMonitor cancellableMonitor, InterruptingProgressMonitor.InterruptableCode<T,E1,E2> codeToInterrupt) throws E1, E2
      Runs the specified code.

      Executes specified code in the current thread. If the specified monitor gets cancelled while the code is still being executed then the current thread is interrupted.

      If the specified monitor is already cancelled then the code is run anyway and has a chance to react to being eventually interrupted.

      After the specified code exits, the current thread would not be interrupted anymore when the specified monitor gets cancelled.

      Type Parameters:
      T - Type of the value returned by the specified code.
      E1 - Type of the exception thrown by the specified code.
      E2 - Type of the exception thrown by the specified code.
      Parameters:
      cancellableMonitor - Monitor that user can cancel and thus request the code block to be interrupted. Must not be null.
      codeToInterrupt - The code to be executed. Must not be null.
      Returns:
      The same value as the specified code block returns.
      Throws:
      E1 - The same exception as the specified code block throws.
      E2 - The same exception as the specified code block throws.