38 lines
923 B
Java
38 lines
923 B
Java
package chapter1.sleepers;
|
|
|
|
import common.TestHarness;
|
|
import org.junit.Test;
|
|
|
|
import java.util.Date;
|
|
|
|
public class Sleeper3b {
|
|
|
|
/* what does this print and does that bother you? */
|
|
public void sleep() {
|
|
synchronized (this) {
|
|
try {
|
|
System.out.printf("[%s] thread %s sleep\n", new Date().toString(), Thread.currentThread().getName());
|
|
|
|
this.wait(1000);
|
|
|
|
System.out.printf("[%s] thread %s wake up\n", new Date().toString(), Thread.currentThread().getName());
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Test
|
|
public void test() {
|
|
TestHarness testHarness = new TestHarness(2);
|
|
Sleeper3b sleeper = new Sleeper3b();
|
|
|
|
testHarness.testMultiThreaded(() -> {
|
|
testHarness.waitForSignal();
|
|
sleeper.sleep();
|
|
});
|
|
|
|
}
|
|
}
|