java - JRuby and Swing Event Dispatch Thread -
java swing tutorials utilize swingutilities.invokelater create gui (like here). why done explained here - swing objects not thread safe. on other hand jruby swing examples i've seen set top frame visible in script (like here - 'guis' directory in archive).
the question is, should follow java pattern of using swingutilities.invokelater() when creating application top frame in jruby scripts?
i have 2 jruby scripts, 1 uses swingutilities.invokelater() , 1 doesn't:
java_import javax.swing.jframe java_import javax.swing.swingutilities java_import java.awt.dimension java_import javax.swing.jbutton javathread = java.lang.thread class topframe < jframe def initialize super init_components() pack() set_visible(true) puts '----------------------------------------' puts 'in jframe constructor:' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list end def init_components() set_default_close_operation(jframe::exit_on_close) set_preferred_size(dimension.new(400, 300)) button = jbutton.new('button') button.add_action_listener |event| puts '----------------------------------------' puts 'in action listener:' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list end get_content_pane.add(button) end end puts '----------------------------------------' puts 'before swingutilties.invokelater():' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list swingutilities.invoke_later topframe.new end puts '----------------------------------------' puts 'after swingutilities.invokelater():' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list the output is:
---------------------------------------- before swingutilties.invokelater(): thread name: main event dispatch thread: false java.lang.threadgroup[name=main,maxpri=10] thread[main,5,main] thread[ruby-0-jit-1,1,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] ---------------------------------------- after swingutilities.invokelater(): thread name: main event dispatch thread: false java.lang.threadgroup[name=main,maxpri=10] thread[main,5,main] thread[ruby-0-jit-1,1,main] thread[awt-eventqueue-0,6,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] ---------------------------------------- in jframe constructor: thread name: awt-eventqueue-0 event dispatch thread: true java.lang.threadgroup[name=main,maxpri=10] thread[awt-eventqueue-0,6,main] thread[destroyjavavm,5,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] ---------------------------------------- in action listener: thread name: awt-eventqueue-0 event dispatch thread: true java.lang.threadgroup[name=main,maxpri=10] thread[awt-eventqueue-0,6,main] thread[destroyjavavm,5,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] before swingutilities.invokelater() called edt doesn't exist after swingutilities.invokelater() edt exists , it's different current thread. in jframe constructor , jbutton action event listener edt current thread.
the other script doesn't utilize swingutilities.invokelater():
java_import javax.swing.jframe java_import javax.swing.swingutilities java_import java.awt.dimension java_import javax.swing.jbutton javathread = java.lang.thread puts '----------------------------------------' puts 'before jframe.new():' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list frame = jframe.new() frame.set_default_close_operation(jframe::exit_on_close) frame.set_preferred_size(dimension.new(400, 300)) button = jbutton.new('button') button.add_action_listener |event| puts '----------------------------------------' puts 'in action listener:' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list end frame.get_content_pane.add(button) frame.pack() puts '----------------------------------------' puts 'before jframe.setvisible():' puts "thread name: #{javathread.current_thread.name}" puts "is event dispatch thread: #{swingutilities.event_dispatch_thread?}" javathread.current_thread.thread_group.list frame.set_visible(true) the output is:
---------------------------------------- before jframe.new(): thread name: main event dispatch thread: false java.lang.threadgroup[name=main,maxpri=10] thread[main,5,main] thread[ruby-0-jit-1,1,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] ---------------------------------------- before jframe.setvisible(): thread name: main event dispatch thread: false java.lang.threadgroup[name=main,maxpri=10] thread[main,5,main] thread[ruby-0-jit-1,1,main] thread[awt-eventqueue-0,6,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] ---------------------------------------- in action listener: thread name: awt-eventqueue-0 event dispatch thread: true java.lang.threadgroup[name=main,maxpri=10] thread[awt-eventqueue-0,6,main] thread[destroyjavavm,5,main] java.lang.threadgroup[name=ruby threads#11483240,maxpri=10] edt created after jframe constructor not current thread. in jbutton event listener edt current thread.
consider jruby other jvm language ... no special care swing/awt conventions built-in - "new" syntax writing swing gui. such care edt in java.
java multithreading swing jruby
No comments:
Post a Comment