用JAVA实现线程等待提示框
来源:互联网 发布:javascript 正则 转义 编辑:程序博客网 时间:2024/05/22 03:24
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>Java语言从其诞生到现在不过短短五年时间,却已经成为全球最热门的语言,Java程序员正成为IT业其它程序员中薪金最高的职员。这一切都应归功于Java良好的特性:简单、面向对象、分布式、平台无关性、可移植性、支持多线程等等。本文将用Java的多线程特性来实现线程等待提示框。1、问题的提出在Java应用程序编程中,有时需要在GUI(图形化用户界面)中处理一些占用系统资源较多,耗费时间较长的事务,例如:与数据库进行大批量数据交换、大数据量的复杂运算、远程连接服务器等等。系统在处理这些事务时,如果还是使用GUI所在的线程,会导致界面冻结,无法刷新,看起来好象系统已经崩溃,这是一个良好的软件系统不允许出现的局面。2、解决问题的途径解决上述问题的方法就是采用Java的多线程特性,为这些耗时又耗资源的事务再开一个线程单独运行,并在GUI处出现提示框“正在执行,请等待”,在线程结束时自动关闭该提示框。这样即避免了上面出现的界面冻结情况,又保证了线程的安全性,是软件开发者上佳的选择。3、具体实现(1)例子这里举一个简单的例子来介绍如何用JAVA实现线程等待提示框。此例实现一个很简单的GUI,根窗体testFrame是一个JFrame(框架)类,在testFrame中放置一个JPanel(面板):testPanel ,最后将一个JButton(按钮):testButton添加到testPanel中。按下testButton,系统开始运行一个模拟的耗时又耗资源的事务:在标准输出设备上显示从1到100000,同时出现“线程正在运行”提示框,一旦事务完成(即线程结束),系统自动关闭该提示框。(2)实现方法为了达到上述功能,可以这样来实现:当按下按钮后,启动一个新的线程来完成事务,即在标准输出设备上显示从1到100000(在代码中通过TestThread类来实现),紧接着再启动一个线程来显示“线程正在运行”提示框(在代码中通过ThreadDiag类来实现)。为了使提示框在TestThread结束后,自行关闭,在TestThread启动后,还启动了一个DisposeDiag线程,这个线程专门用来等待TestThread线程结束后,关闭“线程正在运行”提示框。(3)程序代码及注释① TestFrame类TestFrame是Java运行主程序,用来显示用户界面。
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestFrame extends JFrame { //GUI所需组件 public JPanel testPanel = null; public JButton testButton = null; public JFrame testFrame = null; public TestFrame() { //设置GUI为windows风格 try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { System.out.println(“Exception: ” ex); } testFrame = this; // 初始化GUI Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); setSize(dimensions.width /2, dimensions.height /2); setLocation(dimensions.width/2-dimensions.width/4, dimensions.height/2-dimensions.height/4); testPanel = new JPanel(); testButton = new JButton("开始线程"); testPanel.add(testButton); getContentPane().add(testPanel); //增加按钮testButton事件监听器 testButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { TestThread testThread = new TestThread();//新生成一个处理事务线程 testThread.start();//启动事务线程 (new ThreadDiag(testFrame, testThread , "正在执行,请等待......")).start();//启动等待提示框线程 } }); //增加testFrame事件监听器 addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { //主程序 TestFrame testFrame2 = new TestFrame(); testFrame2.setTitle("线程等待测试"); testFrame2.show(); } }
② TestThread类TestThread类是处理事务线程,即在标准输出设备上显示从1到100000。public class TestThread extends Thread { public void run() { for (int i = 1; i < 100000 ; i ) { System.out.println(i); } } }
③ ThreadDiag类ThreadDiag类用来显示“线程正在运行”提示框。import java.awt.*; import javax.swing.*; public class ThreadDiag extends Thread { private Thread currentThread = null;//实际调用时就是TestThread事务处理线程 private String messages = "";//提示框的提示信息 private JFrame parentFrame = null;//提示框的父窗体 private JDialog clueDiag = null;// “线程正在运行”提示框 private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); private int width = dimensions.width / 4, height = 60; private int left = 0, top = 0; public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages) { this.parentFrame = parentFrame; this.currentThread = currentThread; this.messages = messages; initDiag();//初始化提示框 } protected void initDiag() { clueDiag = new JDialog(parentFrame,"正在执行,请等待...",true); clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR)); JPanel testPanel = new JPanel(); JLabel testLabel = new JLabel(messages); clueDiag.getContentPane().add(testPanel); testPanel.add(testLabel); (new DisposeDiag()).start();//启动关闭提示框线程 } public void run() { //显示提示框 int left = (dimensions.width - width)/2; int top = (dimensions.height - height)/2; clueDiag.setSize(new Dimension(width,height)); clueDiag.setLocation(left, top); clueDiag.show(); } }
④ DisposeDiag类DisposeDiag类用来关闭提示框class DisposeDiag extends Thread{ public void run() { try { currentThread.join();//等待事务处理线程结束 } catch(InterruptedException e) { System.out.println("Exception:" e); } clueDiag.dispose();//关闭提示框}}
注:为了共用变量clueDiag,上述ThreadDiag类和DisposeDiag类放在同一个Java文件内,如果分开存放,只需传递一下参数即可。上述程序在jdk1.3下运行通过。(4)程序运行结果运行结果如下图所示:当事务执行完后,“正在执行”提示框自动关闭。