读书人

事件派发线程SwingUtilitie的应用示例

发布时间: 2012-10-13 11:38:17 作者: rapoo

事件派发线程SwingUtilitie的使用示例和invokeLater与invoikeAndWait的区别

?

在Java中Swing是线程不安全的,是单线程的设计,这样的造成结果就是:只能从事件派发线程访问将要在屏幕上绘制的Swing组件。事件派发线程是调用paint和update等回调方法的线程,它还是事件监听器接口中定义的事件处理方法,例如,ActionListener中的actionPerformed方法在事件派发线程中调用。

  Swing是事件驱动的,所以在回调函数中更新可见的GUI是很自然的事情,比如,有一个按钮被按下,项目列表需要更新时,则通常在与该按钮相关联的事件监听器的actionPerformed方法中来实现该列表的更新,从事件派发线程以外的线程中更新Swing组件是不正常的。

  有时需要从事件派发线程以外的线程中更新Swing组件,例如,在actionPerformed中有很费时的操作,需要很长时间才能返回,按钮激活后需要很长时间才能看到更新的列表,按钮会长时间保持按下的状态只到actionPerformed返回,一般说来耗时的操作不应该在事件处理方法中执行,因为事件处理返回之前,其他事件是不能触发的,界面类似于卡住的状况,所以在独立的线程上执行比较耗时的操作可能更好,这会立即更新用户界面和释放事件派发线程去派发其他的事件。

  SwingUtilities类提供了两个方法:invokeLate和invoteAndWait,它们都使事件派发线程上的可运行对象排队。当可运行对象排在事件派发队列的队首时,就调用其run方法。其效果是允许事件派发线程调用另一个线程中的任意一个代码块。

  只有从事件派发线程才能更新组件

?

view plain

  1. import?java.awt.FlowLayout;??
  2. import?java.awt.event.ActionEvent;??
  3. import?java.awt.event.ActionListener;??
  4. import?javax.swing.JButton;??
  5. import?javax.swing.JFrame;??
  6. import?javax.swing.JProgressBar;??
  7. import?javax.swing.JTextField;??
  8. public?class?SwingThreadTest1?extends?JFrame?{??
  9. ????private?static?final?long?serialVersionUID?=?1L;??
  10. ????private?static?final?String?STR?=?"Completed?:?";??
  11. ????private?JProgressBar?progressBar?=?new?JProgressBar();??
  12. ????private?JTextField?text?=?new?JTextField(10);??
  13. ????private?JButton?start?=?new?JButton("Start");??
  14. ????private?JButton?end?=?new?JButton("End");??
  15. ????private?boolean?flag?=?false;??
  16. ????private?int?count?=?0;??
  17. ????public?SwingThreadTest1()?{??
  18. ????????this.setLayout(new?FlowLayout());??
  19. ????????add(progressBar);??
  20. ????????text.setEditable(false);??
  21. ????????add(text);??
  22. ????????add(start);??
  23. ????????add(end);??
  24. ????????start.addActionListener(new?Start());??
  25. ????????end.addActionListener(new?End());??
  26. ????}??
  27. ??????????
  28. ????private?void?go()?{??
  29. ????????while?(count?<?100)?{??
  30. ????????????try?{??
  31. ????????????????Thread.sleep(100);//这里比作要完成的某个耗时的工作??
  32. ????????????}?catch?(InterruptedException?e)?{??
  33. ????????????????e.printStackTrace();??
  34. ????????????}??
  35. ?????????????????????????//更新进度条和输入框??
  36. ????????????if?(flag)?{??
  37. ????????????????count++;??
  38. ????????????????progressBar.setValue(count);??
  39. ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
  40. ????????????}??
  41. ????????}??
  42. ????}??
  43. ????private?class?Start?implements?ActionListener?{??
  44. ????????public?void?actionPerformed(ActionEvent?e)?{??
  45. ????????????flag?=?true;//设置开始更新的标志??
  46. ????????????go();//开始工作??
  47. ????????}??
  48. ????}??
  49. ????private?class?End?implements?ActionListener?{??
  50. ????????public?void?actionPerformed(ActionEvent?e)?{??
  51. ????????????flag?=?false;//停止??
  52. ????????}??
  53. ????}??
  54. ????public?static?void?main(String[]?args)?{??
  55. ????????SwingThreadTest1?fg?=?new?SwingThreadTest1();??
  56. ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  57. ????????fg.setSize(300,?100);??
  58. ????????fg.setVisible(true);??
  59. ????}??
  60. }??

?

?

progressBar.setValue(count);

text.setText(STR?+ String.valueOf(count) +?"%");

view plain

  1. import?java.awt.FlowLayout;??
  2. import?java.awt.event.ActionEvent;??
  3. import?java.awt.event.ActionListener;??
  4. import?javax.swing.JButton;??
  5. import?javax.swing.JFrame;??
  6. import?javax.swing.JProgressBar;??
  7. import?javax.swing.JTextField;??
  8. public?class?SwingThreadTest2?extends?JFrame?{??
  9. ????private?static?final?long?serialVersionUID?=?1L;??
  10. ????private?static?final?String?STR?=?"Completed?:?";??
  11. ????private?JProgressBar?progressBar?=?new?JProgressBar();??
  12. ????private?JTextField?text?=?new?JTextField(10);??
  13. ????private?JButton?start?=?new?JButton("Start");??
  14. ????private?JButton?end?=?new?JButton("End");??
  15. ????private?boolean?flag?=?false;??
  16. ????private?int?count?=?0;??
  17. ??????
  18. ????GoThread?t?=?null;??
  19. ????public?SwingThreadTest2()?{??
  20. ????????this.setLayout(new?FlowLayout());??
  21. ????????add(progressBar);??
  22. ????????text.setEditable(false);??
  23. ????????add(text);??
  24. ????????add(start);??
  25. ????????add(end);??
  26. ????????start.addActionListener(new?Start());??
  27. ????????end.addActionListener(new?End());??
  28. ????}??
  29. ????private?void?go()?{??
  30. ????????while?(count?<?100)?{??
  31. ????????????try?{??
  32. ????????????????Thread.sleep(100);??
  33. ????????????}?catch?(InterruptedException?e)?{??
  34. ????????????????e.printStackTrace();??
  35. ????????????}??
  36. ????????????if?(flag)?{??
  37. ????????????????count++;??
  38. ????????????????System.out.println(count);??
  39. ????????????????progressBar.setValue(count);??
  40. ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
  41. ????????????}??
  42. ????????}??
  43. ????}??
  44. ????private?class?Start?implements?ActionListener?{??
  45. ????????public?void?actionPerformed(ActionEvent?e)?{??
  46. ????????????flag?=?true;??
  47. ????????????if(t?==?null){??
  48. ????????????????t?=?new?GoThread();??
  49. ????????????????t.start();??
  50. ????????????}??
  51. ????????}??
  52. ????}??
  53. ????//执行复杂工作,然后更新组件的线程??
  54. ????class?GoThread?extends?Thread{??
  55. ????????public?void?run()?{??
  56. ????????????//do?something...??
  57. ????????????go();??
  58. ????????}??
  59. ????}??
  60. ????private?class?End?implements?ActionListener?{??
  61. ????????public?void?actionPerformed(ActionEvent?e)?{??
  62. ????????????flag?=?false;??
  63. ????????}??
  64. ????}??
  65. ????public?static?void?main(String[]?args)?{??
  66. ????????SwingThreadTest2?fg?=?new?SwingThreadTest2();??
  67. ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  68. ????????fg.setSize(300,?100);??
  69. ????????fg.setVisible(true);??
  70. ????}??
  71. }??

?

view plain

  1. import?java.awt.FlowLayout;??
  2. import?java.awt.event.ActionEvent;??
  3. import?java.awt.event.ActionListener;??
  4. import?javax.swing.JButton;??
  5. import?javax.swing.JFrame;??
  6. import?javax.swing.JProgressBar;??
  7. import?javax.swing.JTextField;??
  8. import?javax.swing.SwingUtilities;??
  9. public?class?SwingThreadTest3?extends?JFrame?{??
  10. ????private?static?final?long?serialVersionUID?=?1L;??
  11. ????private?static?final?String?STR?=?"Completed?:?";??
  12. ????private?JProgressBar?progressBar?=?new?JProgressBar();??
  13. ????private?JTextField?text?=?new?JTextField(10);??
  14. ????private?JButton?start?=?new?JButton("Start");??
  15. ????private?JButton?end?=?new?JButton("End");??
  16. ????private?boolean?flag?=?false;??
  17. ????private?int?count?=?0;??
  18. ??????
  19. ????private?GoThread?t?=?null;??
  20. ??????
  21. ????private?Runnable?run?=?null;//更新组件的线程??
  22. ????public?SwingThreadTest3()?{??
  23. ????????this.setLayout(new?FlowLayout());??
  24. ????????add(progressBar);??
  25. ????????text.setEditable(false);??
  26. ????????add(text);??
  27. ????????add(start);??
  28. ????????add(end);??
  29. ????????start.addActionListener(new?Start());??
  30. ????????end.addActionListener(new?End());??
  31. ??????????
  32. ????????run?=?new?Runnable(){//实例化更新组件的线程??
  33. ????????????public?void?run()?{??
  34. ????????????????progressBar.setValue(count);??
  35. ????????????????text.setText(STR?+?String.valueOf(count)?+?"%");??
  36. ????????????}??
  37. ????????};??
  38. ????}??
  39. ????private?void?go()?{??
  40. ????????while?(count?<?100)?{??
  41. ????????????try?{??
  42. ????????????????Thread.sleep(100);??
  43. ????????????}?catch?(InterruptedException?e)?{??
  44. ????????????????e.printStackTrace();??
  45. ????????????}??
  46. ????????????if?(flag)?{??
  47. ????????????????count++;??
  48. ????????????????SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中??
  49. ????????????}??
  50. ????????}??
  51. ????}??
  52. ????private?class?Start?implements?ActionListener?{??
  53. ????????public?void?actionPerformed(ActionEvent?e)?{??
  54. ????????????flag?=?true;??
  55. ????????????if(t?==?null){??
  56. ????????????????t?=?new?GoThread();??
  57. ????????????????t.start();??
  58. ????????????}??
  59. ????????}??
  60. ????}??
  61. ??????
  62. ????class?GoThread?extends?Thread{??
  63. ????????public?void?run()?{??
  64. ????????????//do?something...??
  65. ????????????go();??
  66. ????????}??
  67. ????}??
  68. ????private?class?End?implements?ActionListener?{??
  69. ????????public?void?actionPerformed(ActionEvent?e)?{??
  70. ????????????flag?=?false;??
  71. ????????}??
  72. ????}??
  73. ????public?static?void?main(String[]?args)?{??
  74. ????????SwingThreadTest3?fg?=?new?SwingThreadTest3();??
  75. ????????fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
  76. ????????fg.setSize(300,?100);??
  77. ????????fg.setVisible(true);??
  78. ????}??
  79. }??

?

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类提供这两种方法用于执行事件派发线程中的代码

?

读书人网 >网络基础

热点推荐