副标题#e#
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线程竣事后,封锁“线程正在运行”提示框。
#p#副标题#e#
(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类
#p#分页标题#e#
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)措施运行功效
运行功效如下图所示:
当事务执行完后,“正在执行”提示框自动封锁。