@Override
  public void runWhenProjectIsInitialized(@NotNull final Runnable action) {
    final Application application = ApplicationManager.getApplication();
    if (application == null) return;

    //noinspection SynchronizeOnThis
    synchronized (this) {
      // in tests which simulate project opening, post-startup activities could have been run
      // already.
      // Then we should act as if the project was initialized
      boolean initialized =
          myProject.isInitialized()
              || application.isUnitTestMode() && myPostStartupActivitiesPassed;
      if (!initialized) {
        registerPostStartupActivity(action);
        return;
      }
    }

    Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            if (!myProject.isDisposed()) {
              action.run();
            }
          }
        };
    if (application.isDispatchThread() && ModalityState.current() == ModalityState.NON_MODAL) {
      runnable.run();
    } else {
      application.invokeLater(runnable, ModalityState.NON_MODAL);
    }
  }
  public void runPostStartupActivities() {
    if (postStartupActivityPassed()) {
      return;
    }

    final Application app = ApplicationManager.getApplication();

    if (!app.isHeadlessEnvironment()) {
      checkFsSanity();
      checkProjectRoots();
    }

    runActivities(myDumbAwarePostStartupActivities);

    DumbService.getInstance(myProject)
        .runWhenSmart(
            new Runnable() {
              @Override
              public void run() {
                app.assertIsDispatchThread();

                // myDumbAwarePostStartupActivities might be non-empty if new activities were
                // registered during dumb mode
                runActivities(myDumbAwarePostStartupActivities);

                //noinspection SynchronizeOnThis
                synchronized (StartupManagerImpl.this) {
                  if (!myNotDumbAwarePostStartupActivities.isEmpty()) {
                    while (!myNotDumbAwarePostStartupActivities.isEmpty()) {
                      queueSmartModeActivity(myNotDumbAwarePostStartupActivities.remove(0));
                    }

                    // return here later to set myPostStartupActivitiesPassed
                    DumbService.getInstance(myProject).runWhenSmart(this);
                  } else {
                    myPostStartupActivitiesPassed = true;
                  }
                }
              }
            });

    // otherwise will be stored - we must not create config files in tests
    if (!app.isUnitTestMode()) {
      Registry.get("ide.firstStartup").setValue(false);
    }
  }