首页 > 代码库 > 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次-004

子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次-004

子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次
public class Sub10Main100Loop50Thread {

	public static void main(String[] args) throws InterruptedException {
		
		Business business = new Business();
		new Thread(new Runnable(){
			@Override
			public void run() {
				for(int i=0;i<5;i++){
					business.sub(i);
				}
			}
		}){}.start();
		Thread.sleep(1000);//目的是让子线程先跑
		
		for(int i=0;i<50;i++){
			business.main(i);
		}
	}
}

class Business{
	public synchronized void sub(int i){
		for(int j=0;j<10;j++){
			System.out.println(Thread.currentThread().getName()+ "  "+j);
		}
		this.notify();
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public synchronized void main(int i){
		
		for(int j=0;j<100;j++){
			System.out.println(Thread.currentThread().getName()+ "  "+j);
		}
		
		this.notify();
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

参考:http://blog.csdn.net/xiaoyu714543065/article/details/8257394

 

子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次-004