为什么会出现java.lang.InterruptedException?
import java.awt.*;
import java.applet.*;
public class carton extends Applet implements Runnable{
Thread thd;
int i,j,k;
Image bimg;
Graphics bg;
String namestr[] = new String[5] ;
Image img[]=new Image[5];
MediaTracker mt;
boolean error;
public void init(){
i=-20;j=20;k=-1;
thd=null;
error=false;
try{
//创建媒体跟踪器
mt=new MediaTracker(this);
for (int j1=0; j1<5; j1++) {
namestr[j1] ="./image/"+String.valueOf(j1+1)+".gif" ;
img[j1]=getImage(getCodeBase(),namestr[j1]);
mt.addImage(img[j1],j1);
}
mt.waitForAll();
}
catch(Exception e){
System.out.println("init:"+e.toString());
System.exit(0);
}
//创建脱屏图像
bimg=createImage(this.getWidth(),this.getHeight());
bg=bimg.getGraphics();
}
public void paint(Graphics g){
g.drawImage(img[k],i,j,this);
}
public synchronized void update(Graphics g){
paint(bg);
g.drawImage(bimg,0,0,this);
}
public void run(){
try {
//设置优先级
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
while (true){
i=i+20;k=k+1;
if(k>=5)k=0;
if(i>=500){i=0;j=j+20;}
if(j>=500){i=0;j=20;k=0;}
repaint();
Thread.sleep(1000);
}
}
catch (Exception e){System.out.println("Thead:"+e.toString());
}
}
public void start() {
if (thd == null) {
thd = new Thread(this);
thd.start();
}
}
public void stop() {
bg.dispose();
thd = null;
}
}
问题出在:
如果最小化窗口,再最大化
Thead:java.lang.InterruptedException: sleep interrupted
Thead:java.lang.InterruptedException: sleep interrupted
以后sleep(1000)就根本不起作用了