Re: Help me with threads.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Bruno Jouhier [MVP] (bjouhier_at_club-internet.fr)
Date: 01/21/05


Date: Fri, 21 Jan 2005 22:14:44 +0100


"Jon Skeet [C# MVP]" <skeet@pobox.com> a écrit dans le message de news:
MPG.1c5b4d62f0ae3f8398bc59@msnews.microsoft.com...
> Bruno Jouhier [MVP] <bjouhier@club-internet.fr> wrote:
>> > Just to correct something - Swing and AWT (and possibly SWT as well,
>> > not sure) also require you to only access UI objects in the UI thread.
>> > That's what SwingUtilities.invokeLater/invokeAndWait are for.
>>
>> Then, they have changed things since I used these toolkits. I worked with
>> the first releases (AWT in 96-98, just a little bit of Swing, no SWT but
>> I
>> assumed it was the same) and there was no such thing at the time (but
>> lots
>> of ugly deadlocks in AWT). They probably had to go this way to be able to
>> take advantage of the fast MFC controls when running on Windows, and to
>> get
>> rid of their awful deadlocks.
>
> Swing has always been the same, as far as I'm aware. I'm not sure about
> AWT, but I doubt that they'd make such a breaking change late on. It
> may well not have been well documented, of course...

I looked a bit more into this:

Swing and SWT are not free-threaded (except for some very specific calls in
the Swing toolkit). So, you have to go through the invoke calls if you are
calling from another thread. And I was completely wrong on those (I
extrapolated too quickly from my early AWT experience).

AWT sounds more complex: internally, there is a single thread that accesses
the message queue and the drawing methods, but externally, you are allowed
to call the AWT methods from several threads. This leads to extra complexity
and inefficiencies in the toolkit, which is why a different approach was
taken by Swing and SWT. So, from what I've found on Google (which is not
always very clear), it seems that AWT is still free threaded from the
outside, even if it is single threaded inside.

Actually, it would not be too difficult to turn the WinForms toolkit into a
free threaded toolkit "from the outside", you would just need to rewrite
every public API entry point as:

    foo()
    {
        if (InvokeRequired)
            BeginInvoke(new FooDelegate(internalFoo));
        else
            internalFoo();
    }

The toolkit would then be free threaded "from the outside" but it would be
slower.

Bruno.

>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • Re: JList oder Alternative?
    ... Mit dem Quick und Swing habe ich so meine Probleme. ... In meinem Modell dazu arbeite ich mit drei Sets (selectedSet, ... mich zu sehr mit dem Toolkit zu beschäftigen. ...
    (de.comp.lang.java)
  • Re: swing comments from an ex-C++ motif programmer
    ... possibly with a GUI builder. ... how are you learning Swing? ... into Google Web Toolkit if you are developing AJAX based web apps. ... Otherwise, for regular old Java, Java WebStart is pretty much your cup of tea. ...
    (comp.lang.java.help)
  • Re: swing comments from an ex-C++ motif programmer
    ... What I am after is a toolkit that is built on swing that makes things ... possibly with a GUI builder. ... into Google Web Toolkit if you are developing AJAX based web apps. ... Otherwise, for regular old Java, Java WebStart is pretty much your ...
    (comp.lang.java.help)
  • Re: Java GUI Entwicklung - womit?
    ... Ich möchte SWT wegen des look'n feel des Original-OS. ... NUR DESHALB ist Swing ein Notanker. ... SWT toolkit. ...
    (de.comp.lang.java)