事件派发线程SwingUtilitie的使用示例和invokeLater与invoikeAndWait的区别
?
在Java中Swing是线程不安全的,是单线程的设计,这样的造成结果就是:只能从事件派发线程访问将要在屏幕上绘制的Swing组件。事件派发线程是调用paint和update等回调方法的线程,它还是事件监听器接口中定义的事件处理方法,例如,ActionListener中的actionPerformed方法在事件派发线程中调用。
Swing是事件驱动的,所以在回调函数中更新可见的GUI是很自然的事情,比如,有一个按钮被按下,项目列表需要更新时,则通常在与该按钮相关联的事件监听器的actionPerformed方法中来实现该列表的更新,从事件派发线程以外的线程中更新Swing组件是不正常的。
有时需要从事件派发线程以外的线程中更新Swing组件,例如,在actionPerformed中有很费时的操作,需要很长时间才能返回,按钮激活后需要很长时间才能看到更新的列表,按钮会长时间保持按下的状态只到actionPerformed返回,一般说来耗时的操作不应该在事件处理方法中执行,因为事件处理返回之前,其他事件是不能触发的,界面类似于卡住的状况,所以在独立的线程上执行比较耗时的操作可能更好,这会立即更新用户界面和释放事件派发线程去派发其他的事件。
SwingUtilities类提供了两个方法:invokeLate和invoteAndWait,它们都使事件派发线程上的可运行对象排队。当可运行对象排在事件派发队列的队首时,就调用其run方法。其效果是允许事件派发线程调用另一个线程中的任意一个代码块。
只有从事件派发线程才能更新组件
?
view plain
- import?java.awt.FlowLayout;??
- import?java.awt.event.ActionEvent;??
- import?java.awt.event.ActionListener;??
- import?javax.swing.JButton;??
- import?javax.swing.JFrame;??
- import?javax.swing.JProgressBar;??
- import?javax.swing.JTextField;??
- public?class?SwingThreadTest1?extends?JFrame?{??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????private?static?final?String?STR?=?"Completed?:?";??
- ????private?JProgressBar?progressBar?=?new?JProgressBar();??
- ????private?JTextField?text?=?new?JTextField(10);??
- ????private?JButton?start?=?new?JButton("Start");??
- ????private?JButton?end?=?new?JButton("End");??
- ????private?boolean?flag?=?false;??
- ????private?int?count?=?0;??
- ????public?SwingThreadTest1()?{??
- ????????this.setLayout(new?FlowLayout());??
- ????????add(progressBar);??
- ????????text.setEditable(false);??
- ????????add(text);??
- ????????add(start);??
- ????????add(end);??
- ????????start.addActionListener(new?Start());??
- ????????end.addActionListener(new?End());??
- ????}??
- ??????????
- ????private?void?go()?{??
- ????????while?(count?<?100)?{??
- ????????????try?{??
- ????????????????Thread.sleep(100);//这里比作要完成的某个耗时的工作??
- ????????????}?catch?(InterruptedException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ?????????????????????????//更新进度条和输入框??
- ????????????if?(flag)?{??
- ????????????????count++;??
- ????????????????progressBar.setValue(count);??
- ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
- ????????????}??
- ????????}??
- ????}??
- ????private?class?Start?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?true;//设置开始更新的标志??
- ????????????go();//开始工作??
- ????????}??
- ????}??
- ????private?class?End?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?false;//停止??
- ????????}??
- ????}??
- ????public?static?void?main(String[]?args)?{??
- ????????SwingThreadTest1?fg?=?new?SwingThreadTest1();??
- ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
- ????????fg.setSize(300,?100);??
- ????????fg.setVisible(true);??
- ????}??
- }??
?
?
progressBar.setValue(count);
text.setText(STR?+ String.valueOf(count) +?"%");
view plain
- import?java.awt.FlowLayout;??
- import?java.awt.event.ActionEvent;??
- import?java.awt.event.ActionListener;??
- import?javax.swing.JButton;??
- import?javax.swing.JFrame;??
- import?javax.swing.JProgressBar;??
- import?javax.swing.JTextField;??
- public?class?SwingThreadTest2?extends?JFrame?{??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????private?static?final?String?STR?=?"Completed?:?";??
- ????private?JProgressBar?progressBar?=?new?JProgressBar();??
- ????private?JTextField?text?=?new?JTextField(10);??
- ????private?JButton?start?=?new?JButton("Start");??
- ????private?JButton?end?=?new?JButton("End");??
- ????private?boolean?flag?=?false;??
- ????private?int?count?=?0;??
- ??????
- ????GoThread?t?=?null;??
- ????public?SwingThreadTest2()?{??
- ????????this.setLayout(new?FlowLayout());??
- ????????add(progressBar);??
- ????????text.setEditable(false);??
- ????????add(text);??
- ????????add(start);??
- ????????add(end);??
- ????????start.addActionListener(new?Start());??
- ????????end.addActionListener(new?End());??
- ????}??
- ????private?void?go()?{??
- ????????while?(count?<?100)?{??
- ????????????try?{??
- ????????????????Thread.sleep(100);??
- ????????????}?catch?(InterruptedException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ????????????if?(flag)?{??
- ????????????????count++;??
- ????????????????System.out.println(count);??
- ????????????????progressBar.setValue(count);??
- ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
- ????????????}??
- ????????}??
- ????}??
- ????private?class?Start?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?true;??
- ????????????if(t?==?null){??
- ????????????????t?=?new?GoThread();??
- ????????????????t.start();??
- ????????????}??
- ????????}??
- ????}??
- ????//执行复杂工作,然后更新组件的线程??
- ????class?GoThread?extends?Thread{??
- ????????public?void?run()?{??
- ????????????//do?something...??
- ????????????go();??
- ????????}??
- ????}??
- ????private?class?End?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?false;??
- ????????}??
- ????}??
- ????public?static?void?main(String[]?args)?{??
- ????????SwingThreadTest2?fg?=?new?SwingThreadTest2();??
- ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
- ????????fg.setSize(300,?100);??
- ????????fg.setVisible(true);??
- ????}??
- }??
?
view plain
- import?java.awt.FlowLayout;??
- import?java.awt.event.ActionEvent;??
- import?java.awt.event.ActionListener;??
- import?javax.swing.JButton;??
- import?javax.swing.JFrame;??
- import?javax.swing.JProgressBar;??
- import?javax.swing.JTextField;??
- import?javax.swing.SwingUtilities;??
- public?class?SwingThreadTest3?extends?JFrame?{??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????private?static?final?String?STR?=?"Completed?:?";??
- ????private?JProgressBar?progressBar?=?new?JProgressBar();??
- ????private?JTextField?text?=?new?JTextField(10);??
- ????private?JButton?start?=?new?JButton("Start");??
- ????private?JButton?end?=?new?JButton("End");??
- ????private?boolean?flag?=?false;??
- ????private?int?count?=?0;??
- ??????
- ????private?GoThread?t?=?null;??
- ??????
- ????private?Runnable?run?=?null;//更新组件的线程??
- ????public?SwingThreadTest3()?{??
- ????????this.setLayout(new?FlowLayout());??
- ????????add(progressBar);??
- ????????text.setEditable(false);??
- ????????add(text);??
- ????????add(start);??
- ????????add(end);??
- ????????start.addActionListener(new?Start());??
- ????????end.addActionListener(new?End());??
- ??????????
- ????????run?=?new?Runnable(){//实例化更新组件的线程??
- ????????????public?void?run()?{??
- ????????????????progressBar.setValue(count);??
- ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
- ????????????}??
- ????????};??
- ????}??
- ????private?void?go()?{??
- ????????while?(count?<?100)?{??
- ????????????try?{??
- ????????????????Thread.sleep(100);??
- ????????????}?catch?(InterruptedException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ????????????if?(flag)?{??
- ????????????????count++;??
- ????????????????SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中??
- ????????????}??
- ????????}??
- ????}??
- ????private?class?Start?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?true;??
- ????????????if(t?==?null){??
- ????????????????t?=?new?GoThread();??
- ????????????????t.start();??
- ????????????}??
- ????????}??
- ????}??
- ??????
- ????class?GoThread?extends?Thread{??
- ????????public?void?run()?{??
- ????????????//do?something...??
- ????????????go();??
- ????????}??
- ????}??
- ????private?class?End?implements?ActionListener?{??
- ????????public?void?actionPerformed(ActionEvent?e)?{??
- ????????????flag?=?false;??
- ????????}??
- ????}??
- ????public?static?void?main(String[]?args)?{??
- ????????SwingThreadTest3?fg?=?new?SwingThreadTest3();??
- ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
- ????????fg.setSize(300,?100);??
- ????????fg.setVisible(true);??
- ????}??
- }??
?
class GetInfoThread extends Thread { Runnable getValue,setValue; int value,currentValue; public GetInfoThread(final Test applet){ getValue=new Runnable(){ public void run(){ JProgressBar pb=applet.getProgressBar(); currentValue=pb.getValue(); } }; setValue=new Runnable(){ public void run(){ JProgressBar pb=applet.getProgressBar(); pb.setValue(value); } } } public void run(){ while(true){ try{ Thread.currentThead().sleep(500); value=(int)(Math.random()*100); try{ SwingUtilities.invokeAndWait(getValue);//直到getValue可运行的run方法返回后才返回 }catch(Exception ex){ } if(currentValue!=value){ SwingUtilities.invokeLater(setValue); } } }catch(Exception ex){ } } }
?invokeLater和invoikeAndWait的一个重要区别:可以从事件派发线程中调用invokeLater,却不能从事件派发线程中调用invokeAndWait,从事件派发线程调用invokeAndWait的问题是:invokeAndWait锁定调用它的线程,直到可运行对象从事件派发线程中派发出去并且该可运行的对象的run方法激活,如果从事件派发线程调用invoikeAndWait,则会发生死锁的状况,因为invokeAndWait正在等待事件派发,但是,由于是从事件派发线程中调用invokeAndWait,所以直到invokeAndWait返回后事件才能派发。
ex)actionPerformed();返回的时候事件派发线程才能派发线程,而在actionPerformed中使用invokeAndWait则会导致actionPerformed不能返回。所以也就无法派发invokeAndWait中的线程。
由于Swing是线程不安全的,所以,从事件派发线程之外的线程访问Swing组件是不安全的,SwingUtilities类提供这两种方法用于执行事件派发线程中的代码
?