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();
}
}
----------------解决方案--------------------------------------------------------
没看明白,把run方法放到构造函数里是做什么?到底要实现什么功能?
----------------解决方案--------------------------------------------------------
如果只是实现进度条的从1到100再从100到1的无限循环,也不用wait()和notify()去控制啊。
----------------解决方案--------------------------------------------------------
回复 3楼 ren829
因为是自己看书学习的,想用线程的这两个方法来做,但是这两个方法不怎么会用- -! ----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.*;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;
public class ThreadTest8 extends JFrame implements Runnable{
static int count=0;
JProgressBar jp=null;
JPanel jp1,jp2;
public ThreadTest8(String s){
super(s);
jp1=new JPanel();
jp2=new JPanel();
jp=new JProgressBar();
this.add(jp,BorderLayout.CENTER);
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.SOUTH);
this.setSize(500, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void run() {
while(true){
while(count<100){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
jp.setStringPainted(true);
jp.setValue(count);
jp.setBorderPainted(true);
jp.setBackground(Color.pink);
}
while(count>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count--;
jp.setStringPainted(true);
jp.setValue(count);
jp.setBorderPainted(true);
jp.setBackground(Color.pink);
}
}
}
public static void main(String[]args){
ThreadTest8 tt=new ThreadTest8("wait()语句应用");
Thread t=new Thread(tt);
t.start();
}
}
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;
public class ThreadTest8 extends JFrame implements Runnable{
static int count=0;
JProgressBar jp=null;
JPanel jp1,jp2;
public ThreadTest8(String s){
super(s);
jp1=new JPanel();
jp2=new JPanel();
jp=new JProgressBar();
this.add(jp,BorderLayout.CENTER);
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.SOUTH);
this.setSize(500, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void run() {
while(true){
while(count<100){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
jp.setStringPainted(true);
jp.setValue(count);
jp.setBorderPainted(true);
jp.setBackground(Color.pink);
}
while(count>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count--;
jp.setStringPainted(true);
jp.setValue(count);
jp.setBorderPainted(true);
jp.setBackground(Color.pink);
}
}
}
public static void main(String[]args){
ThreadTest8 tt=new ThreadTest8("wait()语句应用");
Thread t=new Thread(tt);
t.start();
}
}
是不是要这样的效果?
----------------解决方案--------------------------------------------------------
回复 5楼 ren829
嗯 根据您说的 把run方法不要放在构造函数里 我自己把2个线程设成了2个方法 wait()方法和notify()方法也改了改程序貌似就没有问题了 谢谢
----------------解决方案--------------------------------------------------------
好
就是看不懂
----------------解决方案--------------------------------------------------------