Hi Alex,

if you read the next sentence as well, it should become clear:

> Writing data is only allowed from the UI thread,
> and write operations always need to be wrapped in
> a write action with ApplicationManager.getApplication().runWriteAction().
> 
> To pass control from a background thread to the event dispatch thread,
> instead of the standard SwingUtilities.invokeLater(),
> plugins should use ApplicationManager.getApplication().invokeLater().

So your second interpretation is right.

If in doubt, consult the JavaDoc [1] (there is no other site apparently).
For "runReadAction" it says:

> Runs the specified read action. Can be called from any thread.
> The action is executed immediately if no write action is currently
> running, or blocked until the currently running write action completes.

There you see the reason why you need to tell Intellij whether your Runnable is 
reading or writing.
For "runWriteAction" it says:

> Runs the specified write action. Must be called from the
> Swing dispatch thread. The action is executed immediately if no
> read actions are currently running, or blocked until all read
> actions complete.

If you go on reading you might notice "isDispatchThread" which you could
call first to determine whether you even need to call "invokeLater".

Or you could've asked Google (that's what I did).
In the Intellij support forum [2] you'll find the following idiom:

  Application application = ApplicationManager.getApplication();
  if (application.isDispatchThread()) {
    application.runWriteAction(runnable);
  } else {
    application.invokeLater(()-> application.runWriteAction(runnable));
  }

The last statement contains a Lambda expression, which Java 7 does not support,
so it would expand to:

  application.invokeLater(new Runnable() {
    @Override
    public void run() {
      application.runWriteAction(runnable);
    }
  });

Franz

[1] 
https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/application/Application.java
 
[2] 
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206754235-Write-access-is-allowed-inside-write-action-only-see-com-intellij-openapi-application-Application-runWriteAction-

-----Original Message-----
From: Alexander Jakobi [mailto:alexander.jak...@fu-berlin.de] 
Sent: Thursday, September 22, 2016 3:10 PM
To: dpp-devel@lists.sourceforge.net
Subject: [DPP-Devel] IntelliJ Thread Rules

Hey guys,

I'm wondering about a thing in this [1] part of the IntelliJ Platform 
SDK DevGuide:
"Writing data is only allowed from the UI thread, and write operations 
always need to be wrapped in a write action with 
ApplicationManager.getApplication().runWriteAction()."

Does this mean only the UI thread is allowed to write without wrapping 
the commands in a  call 
ofApplicationManager.getApplication().runWriteAction()?
And does this mean when writing from another thread, you will need to 
wrap ApplicationManager.getApplication().runWriteAction() around your 
commands?

OR

Does this mean you have to wrap 
ApplicationManager.getApplication().runWriteAction() around the code you 
want to execute, and wrap another move to the UI thread 
(ApplicationManager.getApplication().invokeLater()) around it. Thus 
writing from a non-UI thread is never ever allowed?

Does anybody know?

Thanks,
Alex


[1] 
http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html


------------------------------------------------------------------------------
_______________________________________________
DPP-Devel mailing list
DPP-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dpp-devel

------------------------------------------------------------------------------
_______________________________________________
DPP-Devel mailing list
DPP-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dpp-devel

Reply via email to