子线程循环10次,主线程循环100次;然后子线程循环10次,主线程循环100次,这样循环50次;

子线程循环10次,主线程循环100次;然后子线程循环10次,主线程循环100次,这样循环50次;

public class CircleThread {
	
	public static void main(String[] args) {
		Fun fun = new Fun();
		new Thread(()->{
			synchronized (fun) {
				for (int j = 0; j < 50; j++) {
					while(fun.x != 1){
						try {
							fun.notify();
							fun.wait();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					for (int i = 0; i < 10; i++) {
						System.out.println(Thread.currentThread().getName() + "---> i = " + i + ";___j = " + j + ";");
					}
					fun.x = 2;
					fun.notify();
				}
			}
		},"Thread-A").start();
		synchronized (fun) {
			for (int k = 0; k < 50; k++) {
				while(fun.x != 2){
					try {
						fun.notify();
						fun.wait();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				for (int x = 0; x < 100; x++) {
					System.out.println(Thread.currentThread().getName() + "---> x = " + x + ";");
				}
				fun.x = 1;
			}
		}
	}
}
class Fun{
	public int x = 1;
}
原文地址:https://www.cnblogs.com/kaka-qiqi/p/14837796.html