一道线程同步面试题

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





一道线程同步面试题,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。