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


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

Re: Opposite of SwingUtil

From "Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this>
Subject Re: Opposite of SwingUtil
Message-ID <roCdnRv_68AEVDXanZ2dnUVZ8sjinZ2d@saix.net> (permalink)
Newsgroups comp.lang.java.gui
References <f3c3f772-e03f-4754-a291-74b234328123@m34g2000hsf.googlegroups.com>
Date 2011-04-27 15:42 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
larkmore@aol.com wrote:
> So I hear a lot about how Swing isn't very thread safe.  So I'm being
> very careful when a method running in a thread other than the event
> dispatching thread changes my GUI widgets and wrap it in the
> SwingUtilities.invokeLater() method.  So what do I do when I want to
> pass information the other direction?  For example:
> 
> public class foo {
> 	JButton button;
> 	int x;
> 	int y;
> 
> 	public foo() {
> 		button = new JButton("F");
> 		button.addActionListener(new ActionListener() {
> 			public void actionPerformed(ActionEvent e) {
> 				//Some code goes here
> 				x++;
> 				//Some other code goes here
> 				y++;
> 			}
> 		});
> 		javax.swing.Timer timer = new javax.swing.Timer(1000), new
> ActionListener() {
> 		  public void actionPerformed(ActionEvent e) {
> 				System.out.println(x + ", " + y);
> 		  }
> 		});
> 		timer.start();
> 	}
> }
> 
> So how do I make sure that both x and y are updated in one atomic (and
> thread safe) operation without running the risk that the timer's
> println() statement will fire in between?  If the answer involves
> using synchronized methods or variables, please include some example
> code as I have yet to really understand the syntax of them.  Thanks
> all!
> -Will

Since you are using the Swing Timer, you are guaranteed that the 
actionPerformed method will be invoked on the EDT. Similarly, your 
ActionListener for your button will only ever be invoked on the EDT. 
Since they will only ever be executed by a single thread, you are 
guaranteed that your Timer will never execute in between x and y.

Rogan

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

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


Thread

Opposite of SwingUtilitie "larkmore" <larkmore@THRWHITE.remove-dii-this> - 2011-04-27 15:42 +0000
  Re: Opposite of SwingUtil "Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this> - 2011-04-27 15:42 +0000
    Re: Opposite of SwingUtil "larkmore" <larkmore@THRWHITE.remove-dii-this> - 2011-04-27 15:42 +0000
      Re: Opposite of SwingUtil "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:42 +0000
        Re: Opposite of SwingUtil "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:42 +0000
        Re: Opposite of SwingUtil "Stanimir Stamenkov" <stanimir.stamenkov@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
          Re: Opposite of SwingUtil "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
      Re: Opposite of SwingUtil "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

csiph-web