public class test {
//定义打印的方法
public synchronized void print(String str){
notify();
System.out.println(str);
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//定义打印奇数的线程类
class odd implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<100;i+=2){
print("o"+i);
}
}
}
//定义打印偶数的线程类
class even implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=2;i<=100;i+=2){
print("e"+i);
}
}
}
public static void main(String[] args) {
test p = new test();
odd o = p.new odd();
even e = p.new even();
new Thread(o).start();
new Thread(e).start();
}
}
结果: