Threading 101: dont sleep while holding a lock

This commit is contained in:
Brian Matzon 2005-05-12 15:44:48 +00:00
parent c5c80aaa57
commit a4e8f36130
1 changed files with 4 additions and 2 deletions

View File

@ -299,19 +299,21 @@ public class DemoBox extends Frame {
} }
public void run() { public void run() {
long sleep_time = 1000;
while (renderThread != null) { while (renderThread != null) {
// check for change of demo // check for change of demo
synchronized (this) { synchronized (this) {
// if no demo set, just sleep // if no demo set, just sleep
if (activeDemo == null) { if (activeDemo == null) {
sleep(1000); sleep_time = 1000;
} else { } else {
// we have a demo! // we have a demo!
sleep(16); sleep_time = 16;
repaint(); repaint();
} }
} }
sleep(sleep_time);
} }
System.out.println("dead"); System.out.println("dead");
} }