...
Code Block | ||
---|---|---|
| ||
class SleepyThread implements Runnable { private static final Object lock = new Object(); private static int steptime = 1; private int myStep; // do stuff when the step raches this value public SleepyThread(int myStep) { this.myStep = myStep; } public void run () { try { synchronized (lock) { while (time != myStep) { lock.wait(); } // ... do stuff time++; lock.notify(); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // Reset interrupted status } } public static void main(String[] args) { for (int i = 5; i > 0; i--) { SleepyThread ms = new SleepyThread(i); new Thread(ms).start(); } } } |
...