package edu.cczu.mythread1;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Commnicate extends JFrame{
Thread t1;
Thread t2;
private int count=0;
final JProgressBar progressBar = new JProgressBar();
public static void main(String[] args) {
init(new Commnicate(),150,100);
}
public Commnicate() {
super();
progressBar.setStringPainted(true);
getContentPane().add(progressBar, BorderLayout.NORTH);
deValue();
addValue();
t1.start();
t2.start();
}
public void addValue(){
t1=new Thread(new Runnable(){
public void run(){
try{
Thread.currentThread().sleep(10000);
System.out.println("线程退出");
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public void deValue(){
t2=new Thread(new Runnable(){
public void run(){
while (true) {
if(count==0){
synchronized (t1) {
try {
System.out.println("t2等待");
t1.wait(); //这里应该等待,为什么t1结束后就被唤醒了?
System.out.println("t2等待结束");
count = 10;
} catch (Exception e) {
e.printStackTrace();
}
}
}
progressBar.setValue(--count);
System.out.println("进度条的当前值为:" + count);
}
}
});
}
public static void init(JFrame frame,int width,int height){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
}
t1.wait(); //这里应该等待,为什么t1结束后就被唤醒了?
当然只是执行了一次,第二次就停在那儿了。 |