スレッド同期面接問題

3883 ワード

package com.wsy.model;

/**
 *  10 , 5 , 10 
 * 
 * @author wsy
 * 
 */
public class TraditionalThreadCommunication {
	static Bussiness bussiness = new Bussiness();

	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= 10; i++) {
					bussiness.sub(i);
				}
			}
		}).start();
		
		for (int i = 1; i <= 10; i++) {
			bussiness.main(i);
		}
		
	}
}

class Bussiness {
	static boolean isThreadSub = true;

	public synchronized void sub(int i) {
		
		while (!isThreadSub) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		for (int j = 1; j <= 10; j++) {
			System.out.println("sub: " + j + " of loop " + i);
		}

		isThreadSub = false;
		this.notify();
		
	}

	public synchronized void main(int i) {
		
		while (isThreadSub) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		for (int j = 1; j <= 5; j++) {
			System.out.println("main: " + j + " of loop " + i);
		}

		isThreadSub = true;
		this.notify();
		
	}
}
出力結果:
sub: 1 of loop 1 sub: 2 of loop 1 sub: 3 of loop 1 sub: 4 of loop 1 sub: 5 of loop 1 sub: 6 of loop 1 sub: 7 of loop 1 sub: 8 of loop 1 sub: 9 of loop 1 sub: 10 of loop 1 main: 1 of loop 1 main: 2 of loop 1 main: 3 of loop 1 main: 4 of loop 1 main: 5 of loop 1 sub: 1 of loop 2 sub: 2 of loop 2 sub: 3 of loop 2 sub: 4 of loop 2 sub: 5 of loop 2 sub: 6 of loop 2 sub: 7 of loop 2 sub: 8 of loop 2 sub: 9 of loop 2 sub: 10 of loop 2 main: 1 of loop 2 main: 2 of loop 2 main: 3 of loop 2 main: 4 of loop 2 main: 5 of loop 2 sub: 1 of loop 3 sub: 2 of loop 3 sub: 3 of loop 3 sub: 4 of loop 3 sub: 5 of loop 3 sub: 6 of loop 3 sub: 7 of loop 3 sub: 8 of loop 3 sub: 9 of loop 3 sub: 10 of loop 3 main: 1 of loop 3 main: 2 of loop 3 main: 3 of loop 3 main: 4 of loop 3 main: 5 of loop 3 sub: 1 of loop 4 sub: 2 of loop 4 sub: 3 of loop 4 sub: 4 of loop 4 sub: 5 of loop 4 sub: 6 of loop 4 sub: 7 of loop 4 sub: 8 of loop 4 sub: 9 of loop 4 sub: 10 of loop 4 main: 1 of loop 4 main: 2 of loop 4 main: 3 of loop 4 main: 4 of loop 4 main: 5 of loop 4 sub: 1 of loop 5 sub: 2 of loop 5 sub: 3 of loop 5 sub: 4 of loop 5 sub: 5 of loop 5 sub: 6 of loop 5 sub: 7 of loop 5 sub: 8 of loop 5 sub: 9 of loop 5 sub: 10 of loop 5 main: 1 of loop 5 main: 2 of loop 5 main: 3 of loop 5 main: 4 of loop 5 main: 5 of loop 5 sub: 1 of loop 6 sub: 2 of loop 6 sub: 3 of loop 6 sub: 4 of loop 6 sub: 5 of loop 6 sub: 6 of loop 6 sub: 7 of loop 6 sub: 8 of loop 6 sub: 9 of loop 6 sub: 10 of loop 6 main: 1 of loop 6 main: 2 of loop 6 main: 3 of loop 6 main: 4 of loop 6 main: 5 of loop 6 sub: 1 of loop 7 sub: 2 of loop 7 sub: 3 of loop 7 sub: 4 of loop 7 sub: 5 of loop 7 sub: 6 of loop 7 sub: 7 of loop 7 sub: 8 of loop 7 sub: 9 of loop 7 sub: 10 of loop 7 main: 1 of loop 7 main: 2 of loop 7 main: 3 of loop 7 main: 4 of loop 7 main: 5 of loop 7 sub: 1 of loop 8 sub: 2 of loop 8 sub: 3 of loop 8 sub: 4 of loop 8 sub: 5 of loop 8 sub: 6 of loop 8 sub: 7 of loop 8 sub: 8 of loop 8 sub: 9 of loop 8 sub: 10 of loop 8 main: 1 of loop 8 main: 2 of loop 8 main: 3 of loop 8 main: 4 of loop 8 main: 5 of loop 8 sub: 1 of loop 9 sub: 2 of loop 9 sub: 3 of loop 9 sub: 4 of loop 9 sub: 5 of loop 9 sub: 6 of loop 9 sub: 7 of loop 9 sub: 8 of loop 9 sub: 9 of loop 9 sub: 10 of loop 9 main: 1 of loop 9 main: 2 of loop 9 main: 3 of loop 9 main: 4 of loop 9 main: 5 of loop 9 sub: 1 of loop 10 sub: 2 of loop 10 sub: 3 of loop 10 sub: 4 of loop 10 sub: 5 of loop 10 sub: 6 of loop 10 sub: 7 of loop 10 sub: 8 of loop 10 sub: 9 of loop 10 sub: 10 of loop 10 main: 1 of loop 10 main: 2 of loop 10 main: 3 of loop 10 main: 4 of loop 10 main: 5 of loop 10