|
@@ -1,7 +1,7 @@
|
|
public class TestWaitAndNotify {
|
|
public class TestWaitAndNotify {
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- WaitThread t1 = new WaitThread(1, 2, 50, false);
|
|
|
|
- WaitThread t2 = new WaitThread(2, 2, 50, false);
|
|
|
|
|
|
+ WaitThread t1 = new WaitThread("t1", 1, 2, 50, false);
|
|
|
|
+ WaitThread t2 = new WaitThread("t2", 2, 2, 50, false);
|
|
t1.start();
|
|
t1.start();
|
|
t2.start();
|
|
t2.start();
|
|
}
|
|
}
|
|
@@ -11,12 +11,14 @@ class MyLock {}
|
|
|
|
|
|
class WaitThread extends Thread {
|
|
class WaitThread extends Thread {
|
|
private static MyLock lock = new MyLock();
|
|
private static MyLock lock = new MyLock();
|
|
|
|
+ private String name;
|
|
private int init;
|
|
private int init;
|
|
private int delta;
|
|
private int delta;
|
|
private int count;
|
|
private int count;
|
|
private boolean isLast;
|
|
private boolean isLast;
|
|
|
|
|
|
- WaitThread(int init, int delta, int count, boolean isLast) {
|
|
|
|
|
|
+ WaitThread(String name, int init, int delta, int count, boolean isLast) {
|
|
|
|
+ this.name = name;
|
|
this.init = init;
|
|
this.init = init;
|
|
this.delta = delta;
|
|
this.delta = delta;
|
|
this.count = count;
|
|
this.count = count;
|
|
@@ -26,7 +28,7 @@ class WaitThread extends Thread {
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
for (int i = 0, num = init; i < count; i++) {
|
|
for (int i = 0, num = init; i < count; i++) {
|
|
- System.out.println(num);
|
|
|
|
|
|
+ System.out.println(name + ": " + num);
|
|
num += delta;
|
|
num += delta;
|
|
if (isLast && i == count - 1) return;
|
|
if (isLast && i == count - 1) return;
|
|
synchronized (lock) {
|
|
synchronized (lock) {
|