top
Loading...
用JAVA實現線程等待提示框

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)程序運行結果

運行結果如下圖所示:


當事務執行完后,“正在執行”提示框自動關閉。


作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗