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


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

Re: Opposite of SwingUtil

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this>
Subject Re: Opposite of SwingUtil
Message-ID <roCdnRv_68AEVDXanZ2dnUVZ8sjinZ2d@saix.net> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <f3c3f772-e03f-4754-a291-74b234328123@m34g2000hsf.googlegroups.com>
References <f3c3f772-e03f-4754-a291-74b234328123@m34g2000hsf.googlegroups.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 53
Date Wed, 27 Apr 2011 15:42:59 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303918979 96.60.20.240 (Wed, 27 Apr 2011 10:42:59 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:42:59 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:3109

Show key headers only | 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