ok, I'm here again to ask one dummy question. I have a custom dialog frame and have progress bar on it. So also I have a genetic algorithm (which works pretty big amount of time).
As I understand all the situation, I need to execute separate threads for algorithm, progress bar and leave the main thread for the dialog (to give him an opportunity to respond to user actions). So I tried to implement this, and got the following:
public class ScheduleDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private static Data data;
private static GeneticEngine geneticEngine;
private static Schedule schedule;
private static JProgressBar progressBar;
private static JLabel statusLabel;
public static void showProgress() {
try {
ScheduleDialog dialog = new ScheduleDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
startScheduleComposing();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void startScheduleComposing() {
BackGroundWorker backGroundWorker = new BackGroundWorker();
GeneticEngineWorker geneticEngineWorker = new GeneticEngineWorker();
new Thread(geneticEngineWorker).start();
new Thread(backGroundWorker).start();
}
private ScheduleDialog() {
this.data = Data.getInstance();
setTitle("");
setModal(true);
setBounds(100, 100, 405, 150);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = getSize();
setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new GridLayout(0, 1, 0, 0));
{
progressBar = new JProgressBar();
progressBar.setBackground(Color.RED);
contentPanel.add(progressBar);
}
{
statusLabel = new JLabel("");
contentPanel.add(statusLabel);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Прервать");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
protected static class BackGroundWorker implements Runnable {
@Override
public void run() {
while(true){
int barValue = 100 - (geneticEngine.getCurrentBestFitness() / geneticEngine.getInitialFitness());
progressBar.setValue(barValue);
progressBar.repaint();
statusLabel.setText(String.valueOf(geneticEngine.getCurrentBestFitness()));
statusLabel.repaint();
try{
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
protected static class GeneticEngineWorker implements Runnable{
@Override
public void run() {
geneticEngine = new GeneticEngine(data);
schedule = geneticEngine.GeneticAlgorithm();
}
}
}
it seems, that new Thread(geneticEngineWorker).start();
is working but nothing happens with progress bar. I think this part of code:
private static void startScheduleComposing() {
BackGroundWorker backGroundWorker = new BackGroundWorker();
GeneticEngineWorker geneticEngineWorker = new GeneticEngineWorker();
new Thread(geneticEngineWorker).start();
new Thread(backGroundWorker).start();
}
we get new Thread(backGroundWorker).start();
executed only after new Thread(geneticEngineWorker).start();
will be stopped.
So if I'm right could you please tell me how code can be reorganized to make the work, and if I'm wrong could you point me to my mistakes?
Thanks everyone in advance!
ADDITION
I debuged it and as I thought, backGroundWorker starts working only after geneticEngineWorker ends or user closes dialog(exception in this case);