wait()和notify()语句的应用 求大神指导
这是我自己看书学习做的练习程序,本来的目的是想实现进度条的从1到100再从100到1的无限循环但是在wait()和notify()语句这里卡住了 貌似synchronized这里也卡住了,
求大神指导
分有点少,望谅解
package 第二十二章;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;
public class 没用8 extends JFrame{
int count = 100;
static Thread A = new Thread();
static Thread B = new Thread();
Container c = getContentPane();
JProgressBar p = new JProgressBar();
public static void fangfa(JFrame frame){
frame.setTitle("wait()语句应用");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setBounds(200, 200, 300, 70);
}
public 没用8(){
c.add(p,BorderLayout.CENTER);
p.setStringPainted(true);
A = new Thread(new Runnable(){
public void run() {
while(count <= 100){
p.setValue(--count);
try{
A.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
}
}
});c.add(p,BorderLayout.CENTER);
p.setStringPainted(true);
B = new Thread(new Runnable(){
public void run() {
while(count <= 100){
p.setValue(++count);
try{
B.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
}
}
});
synchronized(""){
while(true){
try {
A.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
B.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count == 100){
B.notify();
}
if(count == 0){
A.notify();
}
}
}
}
public static void main(String[] args){
fangfa(new 没用8());
A.start();
B.start();
}
}