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


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

Re: [ANN] java.awt.SecondaryLoop

From Steven Simpson <ss@domain.invalid>
Newsgroups comp.lang.java.gui
Subject Re: [ANN] java.awt.SecondaryLoop
Date 2012-03-06 11:30 +0000
Organization Aioe.org NNTP Server
Message-ID <j49h29-ld4.ln1@s.simpson148.btinternet.com> (permalink)
References <jj3qo1$47l$1@dont-email.me>

Show all headers | View raw


On 06/03/12 01:54, Jeff Higgins wrote:
> <http://docs.oracle.com/javase/7/docs/api/java/awt/SecondaryLoop.html>
> <http://sellmic.com/blog/2012/02/29/hidden-java-7-features-secondaryloop/> 
>

How will that work?  I suppose that the enter() call sets off a new EDT, 
and then blocks until exit() is called.  Then it tries to claim back 
responsibility for event dispatch as soon as the current EDT completes 
an event.  Each time the current EDT completes an event, it checks for 
completed SecondaryLoops, i.e. one which has been exit()ed.  At that 
point, it wakes the thread blocked on that SL, and terminates, allowing 
the other thread to take over as EDT.

Hmm, no it doesn't work like that.  Try the progam below.  It creates 
two buttons; we'll call them A and B.  When you click A, it greys out 
for 5 seconds; B for 10 seconds.  If you click A then B before A is 
restored, A has to wait for B to complete.  It seems that A's SL simply 
re-enters the event loop, so the second click is handled by that 
invocation, and must re-enter the loop again.  They can only unwind in 
reverse order.

Am I doing it wrong?

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.SecondaryLoop;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Container;

public class TestSL {
     private static JFrame frame;

     private static JButton makeButton(String text, final int delay) {
         final JButton button = new JButton(text);
         ActionListener al = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     Toolkit tk = Toolkit.getDefaultToolkit();
                     EventQueue eq = tk.getSystemEventQueue();
                     SecondaryLoop loop = eq.createSecondaryLoop();

                     button.setEnabled(false);
                     new Sleeper(delay, loop).start();
                     loop.enter();
                     button.setEnabled(true);
                 }
             };
         button.addActionListener(al);
         return button;
     }

     private static void init() {
         frame = new JFrame("Boo");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         Container content = frame.getContentPane();
         content.setLayout(new GridBagLayout());
         GridBagConstraints c = new GridBagConstraints();
         content.add(makeButton("5 seconds", 5), c);
         content.add(makeButton("10 seconds", 10), c);
         frame.pack();
         frame.setVisible(true);
     }

     public static void main(String[] args) throws Exception {
         Runnable action = new Runnable() {
                 public void run() {
                     init();
                 }
             };

         SwingUtilities.invokeLater(action);
     }

     private static class Sleeper extends Thread {
         private final int seconds;
         private final SecondaryLoop loop;

         public Sleeper(int seconds, SecondaryLoop loop) {
             this.seconds = seconds;
             this.loop = loop;
         }

         public void run() {
             try {
                 Thread.sleep(1000 * seconds);
             } catch (InterruptedException ex) {
                 // Ignore.
             }
             loop.exit();
         }
     }
}



-- 
ss at comp dot lancs dot ac dot uk

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


Thread

[ANN] java.awt.SecondaryLoop Jeff Higgins <jeff@invalid.invalid> - 2012-03-05 20:54 -0500
  Re: [ANN] java.awt.SecondaryLoop markspace <-@.> - 2012-03-05 23:19 -0800
  Re: [ANN] java.awt.SecondaryLoop Steven Simpson <ss@domain.invalid> - 2012-03-06 11:30 +0000
    Re: [ANN] java.awt.SecondaryLoop markspace <-@.> - 2012-03-06 08:28 -0800
      Re: [ANN] java.awt.SecondaryLoop Steven Simpson <ss@domain.invalid> - 2012-03-06 17:00 +0000
        Re: [ANN] java.awt.SecondaryLoop markspace <-@.> - 2012-03-06 10:23 -0800
    Re: [ANN] java.awt.SecondaryLoop Jeff Higgins <jeff@invalid.invalid> - 2012-03-06 12:07 -0500
      Re: [ANN] java.awt.SecondaryLoop markspace <-@.> - 2012-03-06 10:25 -0800

csiph-web