为什么会出现java.lang.InterruptedException?

zyhowe 2003-03-24 03:24:52
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)就根本不起作用了
...全文
9808 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
outlier 2003-03-25
  • 打赏
  • 举报
回复
windows还有一个自带的jvm,你把你的jdk路径放到PATH的最前面试试。
zyhowe 2003-03-24
  • 打赏
  • 举报
回复
问题解决,把thd = new Thread(this);
thd.start(); 放进init()
(在start()里面,每变化一次窗口,将增多一个线程,所以看起来快了)
但是,为什么要那么放呢,为什么start将增多线程呢,以前好像不会?
zyhowe 2003-03-24
  • 打赏
  • 举报
回复
outlier(阿黑哥):立即编写,编译,立即显示的啊

机器没换,而且只装了jdk1.4.0

D:\java\test\WEB-INF\classes>java -version
java version "1.4.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)
kreven 2003-03-24
  • 打赏
  • 举报
回复
outlier 2003-03-24
  • 打赏
  • 举报
回复
可能是编译和运行的JDK的版本不一致。我以前好像也碰到过这个问题。

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧