Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4581

Re: Synchronization when collecting data from the EDT?

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.gui
Subject Re: Synchronization when collecting data from the EDT?
Date 2011-06-06 14:35 +0200
Organization A noiseless patient Spider
Message-ID <isihj5$6pb$1@dont-email.me> (permalink)
References <MdAGp.362$SG4.2@newsfe03.iad> <ish813$mnv$1@dont-email.me> <wMZGp.504$x11.63@newsfe09.iad>

Show all headers | View raw


On 06/06/2011 07:41, Knute Johnson allegedly wrote:
> On 06/05/2011 05:46 PM, markspace wrote:
>> On 6/4/2011 5:38 PM, Knute Johnson wrote:
>>> If you want to remove some data from the EDT and use it in another
>>> thread, does EventQueue.invokeLater() or invokeAndWait() constitute
>>> happens before? I don't think it does,
>>
>> Yes and no.
>>
>> You are correct, SwingUtilities.invokeLater() does NOT specify that it
>> creates a happens-before relationship. I've griped about this before on
>> this group. I think it should, because it's almost impossible to use
>> correctly if it does not.
>>
>> However, if you look at the implementation, you can see it uses an
>> Executor to hand off your runnable to the EDT. And the Executor method
>> invoked does, in fact, specify a happens before relationship. And I
>> can't think of any way to hand off one object (the runnable) to another
>> thread with out creating a happens-before relationship. So as a
>> practical matter, I think you must be ok if you don't synchronize
>> yourself.
>>
>> If feel you must synchronzie (and I don't disagree), the easiest way
>> might be to just create a volatile field in the Runnable and let that
>> synchronize for you.
>>
>> class MyRunnable implements Runnable {
>> public volatile int synch;
>> public void run() {
>> synch++; // read synch
>> // do stuff here...
>>
>> }
>> }
>> ...
>> MyRunnable run = new MyRunnable();
>> run.synch = 42;
>> SwingUtilities.invokeLater( run );
>> ...
>>
>> This is the best I think I can do.
>>
> 
> volatile int sync;
> 
> // in the current thread
> // do something you want visible on the EDT
> 
> ++sync;  // write
> 
> EventQueue.invokeLater(new Runnable() {
>     public void run() {
>         ++sync;  // read
>         // the something is now visible here on EDT
> 
> I think that would work too.
> 

How about a BlockingQueue? More overhead, but makes the intent clearer.
Or a java.util.concurrent.Exchanger.

-- 
DF.
Determinism trumps correctness.

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar


Thread

Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-04 17:38 -0700
  Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-04 17:50 -0700
  Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-05 17:52 +0200
    Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-05 10:47 -0700
      Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-05 23:47 -0400
        Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-05 22:11 -0700
          Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-06 14:31 +0200
          Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-06 23:03 -0400
            Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-07 18:51 +0200
              Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-07 13:38 -0400
                Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-08 00:10 +0200
                Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-07 23:51 -0400
                Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-10 01:17 +0200
                Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-10 01:25 +0200
                Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-09 22:34 -0400
                Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-10 05:22 +0200
                Re: Synchronization when collecting data from the EDT? markspace <-@.> - 2011-06-10 08:04 -0700
                Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-10 21:13 -0400
                Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-12 12:51 -0700
                Re: Synchronization when collecting data from the EDT? markspace <-@.> - 2011-06-07 23:31 -0700
                Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-08 23:07 -0700
                Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-09 18:35 -0400
                Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-12 12:45 -0700
            Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-08 23:05 -0700
              Re: Synchronization when collecting data from the EDT? "John B. Matthews" <nospam@nospam.invalid> - 2011-06-09 14:35 -0400
  Re: Synchronization when collecting data from the EDT? Roedy Green <see_website@mindprod.com.invalid> - 2011-06-05 13:39 -0700
    Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-05 22:17 -0700
      Re: Synchronization when collecting data from the EDT? markspace <-@.> - 2011-06-07 14:51 -0700
  Re: Synchronization when collecting data from the EDT? markspace <-@.> - 2011-06-05 17:46 -0700
    Re: Synchronization when collecting data from the EDT? Knute Johnson <nospam@knutejohnson.com> - 2011-06-05 22:41 -0700
      Re: Synchronization when collecting data from the EDT? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-06-06 14:35 +0200

csiph-web