10072---多线程交替打印奇偶数

public class PrintOdd implements Runnable {
	private int i = 1;

	@Override
	public void run() {
		synchronized (this) {
			while (i <= 10) {
				if (i%2 != 0) {
					try {
						CM.ptl(i);
						i++;
						notify();
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} else {
					try {
						CM.ptl(i);
						i++;
						notify();
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			notify();
		}
	}

	public static void main(String args[]) {
		PrintOdd odd = new PrintOdd();
		Thread thread = new Thread(odd);
		Thread thread2 = new Thread(odd);
		thread.start();
		thread2.start();
	}
}

你可能感兴趣的:(CoreJava)