How to not block ui thread with this code

Hello, I have the following code snippet in my intellij plugin.

        //invoke later because it reads from other threads(debugging executer)         app.invokeLater(new Runnable() {             @Override             public void run() {                 if (isDebugMode) {                     final String initializeMsg = String.format(DEBUG_TCP_MESSAGE, configuration.getRunnerParameters().getPort());                     //this should wait until the deployment states that it's listening to the port                     while (!outputForwarder.toString().contains(initializeMsg)) {                     }                   //do some other code                 }             }         }, ModalityState.NON_MODAL);


so the while loop seems to block the UI thread in my intellij plugin, how can I rewrite this so it doesn't block the UI thread and I wait till the while loop finishes to execute the other code in this method?

3 comments
Comment actions Permalink

If you need your code to be executed on a background thread, you should use Application.executeOnPooledThread() instead of Application.invokeLater() to execute it.

0
Comment actions Permalink

I change it to that and now I get the following error:

    Access is allowed from event dispatch thread only.
           Details: EventQueue.isDispatchThread()=false
           isDispatchThread()=false
           Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@196fe6d6
           Current thread: Thread[ApplicationImpl pooled thread 17,4,main] 1043770989
           SystemEventQueueThread: Thread[AWT-EventQueue-1 14.1.3#IC-141.1010.3, eap:false,6,main] 1894313674
           "AWT-EventQueue-1 14.1.3#IC-141.1010.3, eap:false" prio=0 tid=0x0 nid=0x0 runnable
           java.lang.Thread.State: RUNNABLE
           at com.intellij.diagnostic.IdeErrorsDialog.getSubmitter(IdeErrorsDialog.java:1014)
           at com.intellij.diagnostic.DefaultIdeaErrorLogger.canHandle(DefaultIdeaErrorLogger.java:55)
           at com.intellij.diagnostic.DialogAppender.appendToLoggers(DialogAppender.java:108)
           at com.intellij.diagnostic.DialogAppender$1.run(DialogAppender.java:70)
           at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
           at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)

0
Comment actions Permalink

This does not look like a complete stacktrace. What code exactly are you trying to execute in a background thread?


0

Please sign in to leave a comment.