您的位置:首页 > 编程语言 > Java开发

用JAVA实现线程等待提示框

2007-05-29 10:35 351 查看
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)程序代码及注释


import javax.swing.*;


import java.awt.*;


import java.awt.event.*;




public class TestFrame extends JFrame




...{


private static final long serialVersionUID = -5540123740905037861L;


// GUI所需组件


public JPanel testPanel = null;


public JButton testButton = null;


public JFrame testFrame = null;


public TestFrame()




...{


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();


}


}






//=======================================


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,messages,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()




...{


// 显示提示框


left = (dimensions.width - width)/2;


top = (dimensions.height - height)/2;


clueDiag.setSize(new Dimension(width,height));


clueDiag.setLocation(left, top);


clueDiag.setVisible(true);


}




class DisposeDiag extends Thread




...{


public void run()




...{


try




...{


currentThread.join();//等待事务处理线程结束




}catch(InterruptedException e)...{


System.out.println("Exception:" + e);


}


clueDiag.dispose();//关闭提示框


}


}


}


//====================


class TestThread extends Thread




...{


public void run()




...{


for (int i = 1; i < 100000 ; i++ )




...{


System.out.println(i);


}


}


}

  (4)程序运行结果
  运行结果如下图所示:



  http://download1.csdn.net/down3/20070529/29103501153.jpg
  当事务执行完后,“正在执行”提示框自动关闭。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: