Question about Java Thread

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
 
M

Michael Borgwardt

Steven said:
I've played around Thread on Sun WTK20 sample program "photoalbum"

The problem i have is

Can i use notifyAll to wake up sleep()??...........
No.

it doesn't have interrupt() method to call it....

That's because you need to call it on the Thread object. But
why would you want to do that anyway?
I know sleep() holds the lock, and free the lock until it sleep came out.

Um, no. That sentence makes no sense at all.
Below is the sample code, i simply change run()'s
this.wait() to thread.sleep();
WHY??

As delta is always > 0 , sleep() should wake up itself...

After the given amount of milliseconds, which may be a lot.
even notifyAll() can't wake up the thread sleep(),

No, it can't. That's not what it's for.

sleep() is for letting the thread pause for a fixed length of time,
the possibility of an interruption is not really relevant.
wait() is for letting different threads synchronize their activities;
here the optional timeout is the exceptional case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top