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


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

Re: Synchronization when collecting data from the EDT?

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.gui
Subject Re: Synchronization when collecting data from the EDT?
Date 2011-06-06 23:03 -0400
Organization The Wasteland
Message-ID <nospam-7A4754.23033306062011@news.aioe.org> (permalink)
References <MdAGp.362$SG4.2@newsfe03.iad> <isg8ng$i6u$1@dont-email.me> <8jPGp.40014$Vp.14760@newsfe14.iad> <nospam-0D155A.23474105062011@news.aioe.org> <TjZGp.40183$Vp.29218@newsfe14.iad>

Show all headers | View raw


In article <TjZGp.40183$Vp.29218@newsfe14.iad>,
 Knute Johnson <nospam@knutejohnson.com> wrote:

> On 06/05/2011 08:47 PM, John B. Matthews wrote:
[...]
> > Focusing on invokeAndWait() [1], I understood it to be the standard 
> > way to wait for data to arrive on the EDT. In particular, it 
> > promises to block until the supplied Runnable's run method has been 
> > called on the EDT. I don't understand why the Runnable needs a 
> > synchronized block.
> 
> The problem with using invokeAndWait() is that it won't block if the 
> thread is interrupted and it won't start the Runnable() either.

The InvocationEvent is posted to the event queue and the calling thread 
immediately enters wait(). It's up to the AWT event thread to dispatch 
the enclosed Runnable.

> The synchronized block is to make sure that the data retrieved from the 
> GUI elements on the EDT were visible to the current thread.

Both invokeAndWait() (on the calling thread) and dispatch() (on the EDT) 
synchronize on the (private) AWTInvocationLock.

> I've come up with what I think is a better way for the project I'm 
> working on and that is to use a Semaphore.
> 
> Semaphore sem = new Semaphore(1); [...]

This looks appealing; it reminds me of DF's suggestion of Exchanger.

[...]

> So John, back to your question about the synchronized block, how 
> would you get data from the EDT to your current thread without some 
> sort of synchronization?

Here's my (contrived) sscce. A javax.swing.Timer increments  an int at 
100 Hz, while a java.util.TimerTask samples the value at 1 Hz. I'd like 
the API to be a little more explicit, but it does say that the calling 
thread blocks and the Runnable's dispatch "will happen after all pending 
events are processed."

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/** @author John B. Matthews */
public class TwoTimer extends JPanel {

    private Timer timer = new Timer(10, null);
    private int value;

    public TwoTimer() {
        final JLabel label = new JLabel("0");
        timer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(String.valueOf(value++));
            }
        });
        this.add(label);
    }

    public int getValue() {
        return value;
    }

    public void startSwingTimer() {
        timer.start();
    }

    public void startUtilTimer() {
        final LinkedList<Integer> samples = new LinkedList<Integer>();
        java.util.Timer sampler = new java.util.Timer();
        sampler.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                try {
                    EventQueue.invokeAndWait(new Runnable() {

                        @Override
                        public void run() {
                            samples.add(getValue());
                        }
                    });
                } catch (InterruptedException ex) {
                    ex.printStackTrace(System.err);
                } catch (InvocationTargetException ex) {
                    ex.printStackTrace(System.err);
                }
                System.out.println(samples.getLast());
            }
        }, 1000, 1000);

    }

    private void display() {
        JFrame f = new JFrame("TwoTimer");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TwoTimer tt = new TwoTimer();
                tt.display();
                tt.startSwingTimer();
                tt.startUtilTimer();
            }
        });
    }
}

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next 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