package threadprinttest;
/**
* 多线程交替打印1-100
* @param args
*/
public class threadPrintTest {
private int i = 1;
public static void main(String[] args) {
threadPrintTest obj = new threadPrintTest();
Runnable runnable = new Runnable() {
@Override
public void run() {
while(obj.i < 1000) {
synchronized(this) {
notify();
System.out.println("Thread " + Thread.currentThread().getName() + " " + obj.i++);
try {
//Thread.currentThread();
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
};
new Thread(runnable).start();
new Thread(runnable).start();
}
}
原帖:link.