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


Groups > comp.lang.java.gui > #3108 > unrolled thread

Opposite of SwingUtilitie

Started by"larkmore" <larkmore@THRWHITE.remove-dii-this>
First post2011-04-27 15:42 +0000
Last post2011-04-27 15:43 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.gui


Contents

  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

#3108 — Opposite of SwingUtilitie

From"larkmore" <larkmore@THRWHITE.remove-dii-this>
Date2011-04-27 15:42 +0000
SubjectOpposite of SwingUtilitie
Message-ID<f3c3f772-e03f-4754-a291-74b234328123@m34g2000hsf.googlegroups.com>
  To: comp.lang.java.gui
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

---
 * 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

[toc] | [next] | [standalone]


#3109 — Re: Opposite of SwingUtil

From"Rogan Dawes" <rogan.dawes@THRWHITE.remove-dii-this>
Date2011-04-27 15:42 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<roCdnRv_68AEVDXanZ2dnUVZ8sjinZ2d@saix.net>
In reply to#3108
  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

[toc] | [prev] | [next] | [standalone]


#3110 — Re: Opposite of SwingUtil

From"larkmore" <larkmore@THRWHITE.remove-dii-this>
Date2011-04-27 15:42 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<a78a35a1-bac6-4118-a741-f0d7cd7354a4@n20g2000hsh.googlegroups.com>
In reply to#3109
  To: comp.lang.java.gui
> 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

Hmm.  Good to know that the Swing Timer's actionPerformed() is always
on the EDT, but maybe I used a poor example.  I use the RXTX libraries
a lot to communicate with serial ports, and I'm worried about
asynchronous data coming from and going into those ports.  I have no
idea what thread runs when new data comes in from a serial port, but I
have a very strong hunch it won't be the EDT.  Yes/no?  What should I
do in that case, or any other case where I know for sure or at least
strongly suspect that the thread manipulating the data will not be the
EDT one?
-Will

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3111 — Re: Opposite of SwingUtil

From"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Date2011-04-27 15:42 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<47a8db72$0$3057$7836cce5@newsrazor.net>
In reply to#3110
  To: comp.lang.java.gui
larkmore@aol.com wrote:
>> 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
> 
> Hmm.  Good to know that the Swing Timer's actionPerformed() is always
> on the EDT, but maybe I used a poor example.  I use the RXTX libraries
> a lot to communicate with serial ports, and I'm worried about
> asynchronous data coming from and going into those ports.  I have no
> idea what thread runs when new data comes in from a serial port, but I
> have a very strong hunch it won't be the EDT.  Yes/no?  What should I
> do in that case, or any other case where I know for sure or at least
> strongly suspect that the thread manipulating the data will not be the
> EDT one?
> -Will
Generally, you can repeat the pattern that Swing took (BTW, you should 
use EventQueue.invokeLater, not SwingUtilities)

You can create a "message queue" for your serial port, and have a single 
thread that reads from that queue and writes the data to your port.  You 
would have another thread who's job it is to read from and respond to 
the serial data coming in.  Quite possibly that thread would pass the 
actual data off to something in the event queue.

-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3112 — Re: Opposite of SwingUtil

From"Lew" <lew@THRWHITE.remove-dii-this>
Date2011-04-27 15:42 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<6sOdnUX7hY0XmjTanZ2dnUVZ_oHinZ2d@comcast.com>
In reply to#3111
  To: comp.lang.java.gui
Daniel Pitts wrote:
> larkmore@aol.com wrote:
>>> 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
>>
>> Hmm.  Good to know that the Swing Timer's actionPerformed() is always
>> on the EDT, but maybe I used a poor example.  I use the RXTX libraries
>> a lot to communicate with serial ports, and I'm worried about
>> asynchronous data coming from and going into those ports.  I have no
>> idea what thread runs when new data comes in from a serial port, but I
>> have a very strong hunch it won't be the EDT.  Yes/no?  What should I
>> do in that case, or any other case where I know for sure or at least
>> strongly suspect that the thread manipulating the data will not be the
>> EDT one?
>> -Will
> Generally, you can repeat the pattern that Swing took (BTW, you should 
> use EventQueue.invokeLater, not SwingUtilities)
> 
> You can create a "message queue" for your serial port, and have a single 
> thread that reads from that queue and writes the data to your port.  You 
> would have another thread who's job it is to read from and respond to 
> the serial data coming in.  Quite possibly that thread would pass the 
> actual data off to something in the event queue.

You can also use
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
to invoke jobs off the EDT.

-- 
Lew

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3113 — Re: Opposite of SwingUtil

From"Stanimir Stamenkov" <stanimir.stamenkov@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<foc06g$2mk$1@registered.motzarella.org>
In reply to#3111
  To: comp.lang.java.gui
Tue, 05 Feb 2008 13:56:55 -0800, /Daniel Pitts/:

> (BTW, you should 
> use EventQueue.invokeLater, not SwingUtilities)

"Transcript of Ask the Experts session on Swing" 
<http://java.sun.com/developer/community/askxprt/2006/jl1016.html>:

> *M Dunn*: Starting a Swing app. I've seen a couple of recommended 
> ways:
> 
> public static void main(String[] args)
> {
> 1) SwingUtilities.invokeLater(...
> 2) EventQueue.invokeLater(...
> }
> 
> Which is the better way to start, or perhaps another way.
> 
> *Scott Violet*: Here's the implementation for 
> SwingUtilities.invokeLater:
> 
>  public static void invokeLater(Runnable doRun) {
>     EventQueue.invokeLater(doRun);
>     }
> 
> Looks familiar, eh? So, if you use EQ.invokeLater you have one 
> method call on the stack. Some would argue that using EQ directly 
> avoids the additional classload of SwingUtilities. But internally 
> Swing uses SwingUtilities, so that argument doesn't really hold. So, 
> it comes do to the additional method on the stack. Does that matter 
> for application startup? I hardly think so. In the end I think it 
> comes down to what you prefer. After all these years my fingers 
> can't type anything but SwingUtilities. :)

-- 
Stanimir

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3115 — Re: Opposite of SwingUtil

From"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<47a9f519$0$2971$7836cce5@newsrazor.net>
In reply to#3113
  To: comp.lang.java.gui
Stanimir Stamenkov wrote:
> Tue, 05 Feb 2008 13:56:55 -0800, /Daniel Pitts/:
> 
>> (BTW, you should use EventQueue.invokeLater, not SwingUtilities)
> 
> "Transcript of Ask the Experts session on Swing" 
> <http://java.sun.com/developer/community/askxprt/2006/jl1016.html>:
> 
>> *M Dunn*: Starting a Swing app. I've seen a couple of recommended ways:
>>
>> public static void main(String[] args)
>> {
>> 1) SwingUtilities.invokeLater(...
>> 2) EventQueue.invokeLater(...
>> }
>>
>> Which is the better way to start, or perhaps another way.
>>
>> *Scott Violet*: Here's the implementation for SwingUtilities.invokeLater:
>>
>>  public static void invokeLater(Runnable doRun) {
>>     EventQueue.invokeLater(doRun);
>>     }
>>
>> Looks familiar, eh? So, if you use EQ.invokeLater you have one method 
>> call on the stack. Some would argue that using EQ directly avoids the 
>> additional classload of SwingUtilities. But internally Swing uses 
>> SwingUtilities, so that argument doesn't really hold. So, it comes do 
>> to the additional method on the stack. Does that matter for 
>> application startup? I hardly think so. In the end I think it comes 
>> down to what you prefer. After all these years my fingers can't type 
>> anything but SwingUtilities. :)
> 
 From my understanding, SwingUtilities was a stopgap for Swing to use 
before AWT implemented and used the EventQueue.  It has used the 
EventQueue for long enough that you can use it yourself safely.

I wouldn't say one was so much better than the other that its worth 
considerable consternation trying to figure out which one to use.  I 
prefer and suggest using EventQueue, but I'm not going to search/replace 
someone else codebase to match.

-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3169 — Re: Opposite of SwingUtil

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Opposite of SwingUtil
Message-ID<ljmcr3pqkmmt6upih58bfvg19ac3bpmnju@4ax.com>
In reply to#3110
  To: comp.lang.java.gui
On Tue, 5 Feb 2008 13:16:10 -0800 (PST), larkmore@aol.com wrote,
quoted or indirectly quoted someone who said :

>What should I
>do in that case, or any other case where I know for sure or at least
>strongly suspect that the thread manipulating the data will not be the
>EDT one?

same as any other pair of threads.  You use locks on objects with
synchronized, volatile etc.  You read Doug Lea's book.

See http://mindprod.com/jgloss/thread.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web