// INITIALIZATION ON EDT
// JProgressBar progress = new JProgressBar();
// progress.setStringPainted(true);
// PREPROCESSING
// update progress bar (indeterminate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(true);
progress.setString("Preprocessing...");
}
});
// perform preprocessing (open input file, determine total number of words, etc)
// PROCESSING
// update progress bar (switch to determinate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(false);
}
});
int count = 0;
while (true)
{
// read a word from the input file; exit loop if EOF
// compute soundex representation
// add entry to map (hash table)
// compute percentage completed
count++;
final int percent = count * 100 / total;
// update progress bar on the EDT
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setString("Processing " + percent + "%");
progress.setValue(percent);
}
});
}
// POSTPROCESSING
// update progress bar (switch to indeterminate mode)
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(true);
progress.setString("Postprocessing...");
}
});
// perform postprocessing (close input file, etc)
// DONE!
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progress.setIndeterminate(false);
progress.setString("Done!");
progress.setValue(100);
}
});
建议
SwingUtilities.invokeLater... public void run()...
)获取文件大小并计算每次迭代处理的字节数。这样,您就不必两次循环遍历文件。