【发布时间】:2015-11-25 05:43:16
【问题描述】:
我目前正在学习 Java。我正在创建一个从用户那里获取信息的项目,然后创建 sql 文件。在那个(漫长的)过程中,我使用任务和线程显示了一个显示进度的面板。但是,我发现如果我在开始时等待 10 秒,代码会按预期执行,但如果没有睡眠,程序会立即退出。任何信息都非常感谢!
package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class GUIMainPanel implements ActionListener
{
private static JFrame frame;
public GUIMainPanel()
{
// Code that creates a panel, getting information from the user
}
public static void main(String[] args)
{
GUIMainPanel gui = new GUIMainPanel();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "Continue")
{
Runnable r1 = new Runnable()
{
public void run()
{
// Progress Monitor frame
ProgressMonitor pbd = new ProgressMonitor();
}
};
Runnable r2 = new Runnable()
{
public void run()
{
// Code that creates files and changes the progress
}
};
Thread thread1 = new Thread(r1);
Thread thread2 = new Thread(r2);
thread1.start();
thread2.start();
}
}
}
package GUI;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import Queries.CreateListsQueries;
import java.beans.*;
public class ProgressMonitor implements PropertyChangeListener
{
private static JProgressBar progressBar; // Progress bar showing the progress of the query creation process
private static JLabel creatingQueriesLabel; // Label showing information about the current state of the query
// creation process
private Task task; // Task used to update the progress bar and label based on the current state of the query
// creation process
private static JFrame frame;
/**
* Constructor
*/
public ProgressMonitor()
{
// Code creating the Progress Monitor Panel and displaying it
// Instances of javax.swing.SwingWorker are not reusuable, so
// we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}
class Task extends SwingWorker<Void, Void>
{
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground()
{
int progress = 0;
double tempProgress = 0;
String currentQueryText = "Test";
try
{
// Infamous 10 seconds wait here
Thread.sleep(10000);
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
}
// Initialize progress property.
setProgress(0);
while (progress < 100)
{
progress = GUIMainPanel.getProgress();
setProgress(progress);
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done()
{
Toolkit.getDefaultToolkit().beep();
frame.setCursor(null); // turn off the wait cursor
System.exit(0);
}
}
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt)
{
if ("progress".equals(evt.getPropertyName()))
{
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
if ("currentQueryText".equals(evt.getPropertyName()))
{
String text = (String) evt.getNewValue();
creatingQueriesLabel.setText(text);
}
}
}
【问题讨论】:
-
你的代码中间有
System.exit(0)。是这样叫的吗? -
感谢您的回答!它在任务完成时被调用(或应该被调用),所以当进度> = 100时。
-
老实说,您的代码已经足够复杂,我认为我们需要一个示例来编译和演示问题。 See here.我不认为任何人会通过阅读代码找到它。
-
e.getActionCommand() == "Continue"错误;这应该是"Continue".equals(e.getActionCommand())。 -
删除了部分代码以使其更清晰。问题是当 Task 被执行时,它会立即完成(立即调用 done() 方法)。但是,如果我尝试调试 doInBackground() 方法,我无法重现该问题,因为一切运行顺利。
标签: java multithreading task