【发布时间】:2013-05-14 00:50:42
【问题描述】:
我有一个Thread 正在运行,而在thread 工作时我想显示一个JOptionPane.showMessageDialog 表示应用程序正在工作并且在线程停止后,JOptionPane 将自动关闭并且OK按钮将一直停用。我的主要代码:
class Index {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
while (ob1.t.isAlive()){
JOptionPane.showMessageDialog(null,"Thread One is alive");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}}
当我运行它时,JOptionPane 显示,但除非我按下 OK 按钮,否则它不会自动关闭。
class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname) {
this.name = threadname;
this.t = new Thread(this, name);
System.out.println("New thread: " + t);
this.t.start(); // Start the thread
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");}
System.out.println(name + " exiting.");
}}
【问题讨论】:
标签: java multithreading swing joptionpane