package com.atguigu.thread;/** * 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, * 接着再回到主线程又循环100,如此循环50次,请写出程序。 * * @author Zeng_X_C@163.com * * 下午8:21:11 */public class ThreadTest02 { private static boolean bShouldMain = false; public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 50; i++) { synchronized (ThreadTest02.class) { if (bShouldMain) { try { ThreadTest02.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 0; j < 10; j++) { System.out.println(Thread.currentThread().getName() + "i=" + i + ",j=" + j); } bShouldMain = true; ThreadTest02.class.notify(); } } } }).start(); for (int i = 0; i < 50; i++) { synchronized (ThreadTest02.class) { if (!bShouldMain) { try { ThreadTest02.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 0; j < 100; j++) { System.out.println(Thread.currentThread().getName() + "i=" + i + ",j=" + j); } bShouldMain = false; ThreadTest02.class.notify(); } } }}