S
Steven Lien
I've played around Thread on Sun WTK20 sample program "photoalbum"
The problem i have is
Can i use notifyAll to wake up sleep()??...........
it doesn't have interrupt() method to call it....
I know sleep() holds the lock, and free the lock until it sleep came out.
But seems when i notifyAll() it while in sleep() time, system hangs...
sleep doesn't even wake up, even the time reaches the wake up time....
Below is the sample code, i simply change run()'s
this.wait() to thread.sleep();
As delta is always > 0 , sleep() should wake up itself...
even notifyAll() can't wake up the thread sleep(),
but the system is hangs.....
Anyone know why??
and help will be appreciated...
protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case FIRE:
// Use FIRE to toggle the activity of the thread
if (thread == null) {
thread = new Thread(this);
thread.start();
} else {
synchronized (this) {
this.notifyAll();
thread = null;
}
}
break;
}
}
public void run() {
Thread mythread = Thread.currentThread();
long scheduled = System.currentTimeMillis();
statsTime = scheduled;
paintTime = scheduled;
frameCount = 0;
frameRate = 0;
while (thread == mythread) {
synchronized (this) {
try {
...
scheduled += speeds[speed];
long delta = scheduled - paintTime;
if (delta > 0) {
thread.sleep(delta); <-- i change the this.wait() to
<-- thread.sleep();
}
next();
repaint();
serviceRepaints();
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
The original the sample code can get from WTK20
Best regards
Steven Lien
The problem i have is
Can i use notifyAll to wake up sleep()??...........
it doesn't have interrupt() method to call it....
I know sleep() holds the lock, and free the lock until it sleep came out.
But seems when i notifyAll() it while in sleep() time, system hangs...
sleep doesn't even wake up, even the time reaches the wake up time....
Below is the sample code, i simply change run()'s
this.wait() to thread.sleep();
As delta is always > 0 , sleep() should wake up itself...
even notifyAll() can't wake up the thread sleep(),
but the system is hangs.....
Anyone know why??
and help will be appreciated...
protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case FIRE:
// Use FIRE to toggle the activity of the thread
if (thread == null) {
thread = new Thread(this);
thread.start();
} else {
synchronized (this) {
this.notifyAll();
thread = null;
}
}
break;
}
}
public void run() {
Thread mythread = Thread.currentThread();
long scheduled = System.currentTimeMillis();
statsTime = scheduled;
paintTime = scheduled;
frameCount = 0;
frameRate = 0;
while (thread == mythread) {
synchronized (this) {
try {
...
scheduled += speeds[speed];
long delta = scheduled - paintTime;
if (delta > 0) {
thread.sleep(delta); <-- i change the this.wait() to
<-- thread.sleep();
}
next();
repaint();
serviceRepaints();
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
The original the sample code can get from WTK20
Best regards
Steven Lien