码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-02-06 14:54:00      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:new t   nbsp   rgs   ide   out   ace   current   not   auto   

子线程循环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

标签:new t   nbsp   rgs   ide   out   ace   current   not   auto   

原文地址:http://www.cnblogs.com/ysloong/p/6370104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!