import java.awt.*;
import java.awt.event.*;public class BollTest
{
public static void main(String[] args)
{ MyFrame frame=new MyFrame();
frame.setBounds(10, 10, 500, 500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}}
class MyFrame extends Frame implements Runnable
{ Thread 红色球,黄色球;
MyCanvas red ,blue;
double t=0;
MyFrame()
{ 红色球=new Thread(this);
黄色球=new Thread(this);
red=new MyCanvas(Color.red);
blue=new MyCanvas(Color.blue);
setLayout(null);
add(red);
add(blue);
red.setLocation(60,100);
blue.setLocation(60,100);
红色球.start();
黄色球.start();
}
public void run()
{ try{Thread.sleep(5000);}
catch(Exception e){}
while(true)
{ t=t+0.2;
if(t>20)t=0;
if(Thread.currentThread()==红色球)
{int x=60;
int h=(int)(1.0/2*t*t*3.8)+60;
red.setLocation(x,h);
try
{ 红色球.sleep(50);
}
catch(InterruptedException e){}
}
else if(Thread.currentThread()==黄色球)
{ int x=60+(int)(26*t);
int h=(int)(1.0/2*t*t*3.8)+60;
blue.setLocation(x,h);
try
{ 黄色球.sleep(50);
}
catch(InterruptedException e){}
}
}
}
}
class MyCanvas extends Canvas
{ Color c;
MyCanvas(Color c)
{ setSize(20,20);
this.c=c;
}
public void paint(Graphics g)
{ g.setColor(c);
g.fillOval(0, 0, 20, 20);
}}
上面的程序模拟自由落体和平抛运动,当线程使用Cpu资源时,休眠5S后在运行,红色球.黄色球轮流使用cpu资源。我的问题是:红色球.黄色球轮流使用cpu资源时,都是自动调用run()方法,休眠5s在继续。在线程休眠时, red.setLocation(60,100); blue.setLocation(60,100); 也应该轮流显示。但实际是,显示了一个位置后,两个球一直是显示while()循环里面的位置,线程一直在while里面执行,没有在调用Thread.sleep(5000)了,这个该怎么解释???
import java.awt.event.*;public class BollTest
{
public static void main(String[] args)
{ MyFrame frame=new MyFrame();
frame.setBounds(10, 10, 500, 500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}}
class MyFrame extends Frame implements Runnable
{ Thread 红色球,黄色球;
MyCanvas red ,blue;
double t=0;
MyFrame()
{ 红色球=new Thread(this);
黄色球=new Thread(this);
red=new MyCanvas(Color.red);
blue=new MyCanvas(Color.blue);
setLayout(null);
add(red);
add(blue);
red.setLocation(60,100);
blue.setLocation(60,100);
红色球.start();
黄色球.start();
}
public void run()
{ try{Thread.sleep(5000);}
catch(Exception e){}
while(true)
{ t=t+0.2;
if(t>20)t=0;
if(Thread.currentThread()==红色球)
{int x=60;
int h=(int)(1.0/2*t*t*3.8)+60;
red.setLocation(x,h);
try
{ 红色球.sleep(50);
}
catch(InterruptedException e){}
}
else if(Thread.currentThread()==黄色球)
{ int x=60+(int)(26*t);
int h=(int)(1.0/2*t*t*3.8)+60;
blue.setLocation(x,h);
try
{ 黄色球.sleep(50);
}
catch(InterruptedException e){}
}
}
}
}
class MyCanvas extends Canvas
{ Color c;
MyCanvas(Color c)
{ setSize(20,20);
this.c=c;
}
public void paint(Graphics g)
{ g.setColor(c);
g.fillOval(0, 0, 20, 20);
}}
上面的程序模拟自由落体和平抛运动,当线程使用Cpu资源时,休眠5S后在运行,红色球.黄色球轮流使用cpu资源。我的问题是:红色球.黄色球轮流使用cpu资源时,都是自动调用run()方法,休眠5s在继续。在线程休眠时, red.setLocation(60,100); blue.setLocation(60,100); 也应该轮流显示。但实际是,显示了一个位置后,两个球一直是显示while()循环里面的位置,线程一直在while里面执行,没有在调用Thread.sleep(5000)了,这个该怎么解释???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货