Javafx embedded in swing multithreaded toolkit issue

Pedro Duque Vieira pedro.duquevieira at gmail.com
Wed Mar 21 14:31:48 PDT 2012


It's a little hard to describe here the various problems I'm having because
of this multithreaded toolkit issue.

I do use invokeLater and runLater, the problem as you mention lies on the
synchronization between this two contexts.
Basically you're faced with the same problem as programmers had with the
multithreaded AWT toolkit, which had to be rewritten to be single threaded
because of the difficulties this scenario brought to programmers.

Graham Hamilton (former Sun employee) describes this very well on his blog
post:

*"I believe you can program successfully with multi-threaded GUI toolkits
if the toolkit is very carefully designed; if the toolkit exposes its
locking methodology in gory detail; if you are very smart, very careful,
and have a global understanding of the whole structure of the toolkit. If
you get one of these things slightly wrong, things will mostly work, but
you will get occasional hangs (due to deadlocks) or glitches (due to
races). This multithreaded approach works best for people who have been
intimately involved in the design of the toolkit. *

* Unfortunately I don't think this set of characteristics scale to
widespread commercial use. What you tend to end up with is normal smart
programmers building apps that don't quite work reliably for reasons that
are not at all obvious. So the authors get very disgruntled and frustrated
and use bad words on the poor innocent toolkit. (Like me when I first
started using AWT. Sorry!)"*


Best regards,

On Wed, Mar 21, 2012 at 7:12 PM, Anthony Petrov
<anthony.petrov at oracle.com>wrote:

> To rewrite your app completely in FX? :)
>
> Seriously though, could you briefly describe the exact problem that you're
> trying to resolve?
>
> As a general guideline to writing GUI apps, it is always recommended to
> not block the event processing thread under any circumstances. Please note
> that this requirement is true for almost every GUI toolkit out there,
> including native GUI APIs. Both Swing and FX are no exceptions. In other
> words, operations such as invokeAndWait() on the event thread should be
> avoided at all costs.
>
> Indeed, in case of embedding FX scenes into Swing we have two unrelated
> GUI toolkits running at the same app, and as such synchronization between
> the states of components in these two toolkits is a challenging task. We,
> FX developers, do realize this. However, there doesn't seem to exist a
> simple solution for this issue. Best advice is to try and use the
> invokeLater()/runLater() kinds of methods only.
>
> --
> best regards,
> Anthony
>
>
> On 3/21/2012 10:58 PM, Pedro Duque Vieira wrote:
>
>> Hi Anthony,
>>
>> Ok, I see.. So is there any solution to this problem?
>>
>> Thanks,
>>
>> On Wed, Mar 21, 2012 at 6:55 PM, Anthony Petrov <
>> anthony.petrov at oracle.com <mailto:anthony.petrov at oracle.**com<anthony.petrov at oracle.com>>>
>> wrote:
>>
>>    Hi Pedro,
>>
>>    Tooltips and combo-box popups are examples of scenes that use
>>    top-level windows. Even if the component showing the popup is
>>    embedded into a JFXPanel, its popups are always top-level windows.
>>
>>    --
>>    best regards,
>>    Anthony
>>
>>
>>    On 3/21/2012 8:51 PM, Pedro Duque Vieira wrote:
>>
>>        ... or couldn't it be a requirement, to have scenes only embedded
>> on
>>        JFXPanel?
>>
>>        On Wed, Mar 21, 2012 at 2:04 PM, Pedro Duque Vieira <
>>        pedro.duquevieira at gmail.com
>>        <mailto:pedro.duquevieira@**gmail.com<pedro.duquevieira at gmail.com>>>
>> wrote:
>>
>>            Hi Artem, thanks for the reply.
>>
>>
>>            If FX lived on Swing event dispatch thread, all the FX
>>            top-level windows
>>
>>                (that are not embedded into JFXPanel) would never
>>                receive any events.
>>
>>            In this scenario (embedding javafx in swing), what
>>            situations would there
>>            be on which top level windows aren't embedded into JFXPanel?
>>
>>            I think this problem is very important. If things continue
>>            as they are,
>>            embedding javafx into swing will be virtually impossible as
>>            this blog post
>>            illustrates:
>>            http://weblogs.java.net/blog/_**_kgh/archive/2004/10/__**
>> multithreaded_t.html<http://weblogs.java.net/blog/__kgh/archive/2004/10/__multithreaded_t.html>
>>
>>            <http://weblogs.java.net/blog/**kgh/archive/2004/10/**
>> multithreaded_t.html<http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html>
>> >
>>
>>            Cheers,
>>
>>
>>            On Wed, Mar 21, 2012 at 9:23 AM, Artem Ananiev
>>            <artem.ananiev at oracle.com
>>            <mailto:artem.ananiev at oracle.**com <artem.ananiev at oracle.com>
>> >>__wrote:
>>
>>
>>                On 3/21/2012 6:17 AM, Pedro Duque Vieira wrote:
>>
>>                    Hi,
>>
>>                    I've been working for a while on a swing app that
>>                    has a javafx scene
>>                    embedded on it.
>>
>>                    So in this scenario we have two UI threads, one for
>>                    the swing part and
>>                    one
>>                    for the javafx scene (the application thread).
>>                    This situation creates a multithreaded ui toolkit
>>                    which is basically a
>>                    nightmare for development.
>>
>>                    I don't know if this is possible but it I think it
>>                    would be best to merge
>>                    the swing and the javafx application thread into one
>>                    when on this
>>                    scenario.
>>
>>                In JavaFX/SWT bridge (FXCanvas) we do exactly this: FX
>>                application thread
>>                is the same as SWT event thread. Unfortunately, in
>>                JavaFX/Swing case it is
>>                not possible.
>>
>>                In a few words, Swing event dispatch thread only pumps
>>                Java events. All
>>                the native events are dispatched on another thread,
>>                which is called AWT
>>                toolkit thread. This thread cannot be accessed from
>>                outside of AWT, it's
>>                not a part of public API. If FX lived on Swing event
>>                dispatch thread, all
>>                the FX top-level windows (that are not embedded into
>>                JFXPanel) would never
>>                receive any events.
>>
>>                Thanks,
>>
>>                Artem
>>
>>
>>                 One other possible solution that comes to my mind  and
>>                also a much
>>
>>                    simpler
>>                    and quicker one is to add a
>>                    Platform.invokeAndWait(like what exists on
>>                    swing) alongside the Platform.runLater to the javafx
>>                    API.
>>                    And each time I want to invoke something on the
>>                    javafx App thread from
>>                    the
>>                    swing ui thread I call invokeAndWait instead of
>>                    runLater. So effectively
>>                    the two UI threads become only one, because they are
>>                    not running
>>                    concurrently but sequentially instead.
>>
>>
>>            --
>>            Pedro Duque Vieira
>>
>>
>>
>>
>>
>>
>>
>> --
>> Pedro Duque Vieira
>>
>


-- 
Pedro Duque Vieira


More information about the openjfx-dev mailing list