import java.awt.BorderLayout;
public class Synchronicle extends JFrame { private JPanel contentPane;
JButton button;
JButton button_1;
static JLabel label1;
static JLabel label2;
 int c1=0;
static int c2=0;
SwingWorker<Integer, Integer> sw1,sw2; /**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Synchronicle frame = new Synchronicle();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});


} /**
 * Create the frame.
 */
public Synchronicle() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

button = new JButton("\u4E00\u53F7");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sw1=new SwingWorker<Integer, Integer>(){ protected Integer doInBackground() throws Exception {
while(!isCancelled())
{
c1=c1+1;
publish(c1);
Thread.sleep(10);
// System.out.println(c1);
}
return null;
} @Override
protected void done() {
// TODO Auto-generated method stub
super.done();
} @Override
protected void process(List<Integer> chunks) {
int temp=chunks.get(0);
label1.setText(""+temp);
System.out.println(temp);
super.process(chunks);
}



};
sw1.execute();
}
});



button.setBounds(75, 186, 93, 23);
contentPane.add(button);

button_1 = new JButton("\u4E8C\u53F7");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sw2=new SwingWorker<Integer, Integer>(){ @Override
protected Integer doInBackground() throws Exception {
// TODO Auto-generated method stub
while(!isCancelled())
{
c2=c2+1;
publish(c2);
Thread.sleep(1000);
}
return null;
} @Override
protected void done() {
// TODO Auto-generated method stub
super.done();
} @Override
protected void process(List<Integer> chunks) {
// TODO Auto-generated method stub
int temp=chunks.get(0);
label2.setText(""+temp);
super.process(chunks);
}

};
sw2.execute();
}
});
button_1.setBounds(271, 186, 93, 23);
contentPane.add(button_1);

label1 = new JLabel("0");
label1.setBounds(86, 84, 54, 15);
contentPane.add(label1);

label2 = new JLabel("0");
label2.setBounds(286, 84, 54, 15);
contentPane.add(label2);
}
}
每次publish之后,有问题啊,如果doinbackground睡眠时间太短,那么不是每次都能publish到