PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitComment(@NotNull PsiComment element) { if (element.getText().equals("//XXX")) { holder.registerProblem(element, MyFastAnnotator.SWEARING); } } @Override public void visitFile(@NotNull PsiFile file) { // use this contrived form to be able to bail out immediately by modifying toSleepMs in the other thread while (toSleepMs.addAndGet(-100) > 0) { TimeoutUtil.sleep(100); } } }; }