事情就是这样:我有一个Java应用程序主窗口的JFrame,它包含一个包含多个JProgressBar的面板.我想为每个JProgressBar启动一个Thread,它将自己启动另一个Threads.当任何这个“辅助”线程完成时,我想在我的JFrame中更新JProgressBar.此外,在安排所有这些之前,因为我不希望用户能够点击JFrame上的任何内容,我还想在JFrame中设置一些按钮的setEnabled(false).简单?
ActivarBotones abFalse = new ActivarBotones(false);
abFalse.start();
try {
abFalse.join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
EstablecerConexiones ec = new EstablecerConexiones();
ec.start();
try {
ec.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ActivarBotones abTrue = new ActivarBotones(true);
abTrue.start();
两个问题.
>如果我从上面运行代码,则没有更新任何内容.如果我只启动ec线程,一切都能正常工作.
>我对同步知之甚少,不知道我应该怎么做才能同时启动所有“主”线程.
有线索吗?
解决方法:
这是一个小样本,我不认为它回答了你所有的问题,但它展示了基本的概念(有一个生病的妻子和照顾一个6个月大的孩子,用一只手打字:P)
public class ThreadedProgress {
public static void main(String[] args) {
new ThreadedProgress();
}
public ThreadedProgress() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JPanel progressPane = new JPanel(new GridBagLayout());
JProgressBar progressBar = new JProgressBar(0, 100);
progressPane.add(progressBar);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(progressPane);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Thread(new MainThread(progressBar)).start();
}
});
}
public interface CallBack {
public void done(Runnable runnable);
}
public static class MainThread implements CallBack, Runnable {
public static final Object UPDATE_LOCK = new Object();
public static final Object WAIT_LOCK = new Object();
private List<Runnable> running = new ArrayList<>(25);
private List<Runnable> completed = new ArrayList<>(25);
private final JProgressBar progressBar;
public MainThread(JProgressBar progressBar) {
this.progressBar = progressBar;
}
@Override
public void done(Runnable runnable) {
synchronized (UPDATE_LOCK) {
running.remove(runnable);
completed.add(runnable);
}
int count = running.size() + completed.size();
updateProgress(completed.size(), count);
synchronized (WAIT_LOCK) {
WAIT_LOCK.notify();
}
}
protected void updateProgress(final int value, final int count) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
int progress = Math.round(((float) value / (float) count) * 100f);
progressBar.setValue(progress);
}
});
}
@Override
public void run() {
for (int index = 0; index < 5; index++) {
ChildSpawn spawn = new ChildSpawn(this);
running.add(spawn);
}
for (Runnable runnable : running) {
new Thread(runnable).start();
}
while (running.size() > 0) {
synchronized (WAIT_LOCK) {
try {
WAIT_LOCK.wait();
} catch (InterruptedException ex) {
}
}
}
System.out.println("I'm all done");
}
}
public static class ChildSpawn implements Runnable {
private CallBack callBack;
public ChildSpawn(CallBack callBack) {
this.callBack = callBack;
}
@Override
public void run() {
try {
Thread.sleep((long)Math.round(Math.random() * 5000));
} catch (InterruptedException ex) {
}
callBack.done(this);
}
}
}