日期:2014-05-20 浏览次数:20714 次
package test;
public class Test implements Runnable {
private static int iValue = 0;
private static Object iLock = new Object();
private final int iStepSize;
public Test(final int stepSize) {
iStepSize = stepSize;
}
@Override
public void run() {
while (true) {
System.out.print(iValue);
iValue += iStepSize;
synchronized (iLock) {
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) {
}
}
}
}
public static void main(final String[] args) throws Exception {
Thread t1 = new Thread(new Test(1));
Thread t2 = new Thread(new Test(-1));
t1.start();
Thread.sleep(100);
t2.start();
Thread.sleep(500);
}
}