线程同步的问题,急!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProgressBar implements ActionListener
{
JFrame frame=null;
JProgressBar progressbar;
JLabel label;
JButton b;
MyThread thread;
private int len = 20;
private int size = 100;
private int value = 0;
private boolean doing = true;
public ProgressBar()
{
frame=new JFrame("安装");
frame.setBounds(100, 100, 400, 130);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel=frame.getContentPane();
label=new JLabel("",JLabel.CENTER);
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.setPreferredSize(new Dimension(300,20));
progressbar.setBorderPainted(true);
progressbar.setBackground(Color.pink);
thread = new MyThread();
JPanel panel=new JPanel();
b=new JButton("安装");
b.setForeground(Color.blue);
b.addActionListener(this);
panel.add(b);
contentPanel.add(panel,BorderLayout.NORTH);
contentPanel.add(progressbar,BorderLayout.CENTER);
contentPanel.add(label,BorderLayout.SOUTH);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
thread.start();
value += len/(float)size*100;
System.out.println("...."+value);
}
}
//线程类
public class MyThread extends Thread
{
public MyThread()
{
}
public void run()
{
while(doing)
{
progressbar.setValue(value);
}
}
}
public static void main(String[] args)
{
ProgressBar app=new ProgressBar();
}
}
代码如上,程序很简单,就是单击按钮,进度条显示相应进度,由于进度条的进度值是在单击按钮后实时更新的,所以要实现同步更新,但现在问题是不知道怎么写了,刚开始学习线程,所以不太明白,请大家帮帮忙,指导一下
------解决方案--------------------