您的位置:首页 > 职场人生

面试题:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程循环100次,如此循环50次,请写出程序

2012-12-04 20:29 585 查看
public class ThreadTest {

public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

new ThreadTest().init();

}

public void init() throws InterruptedException

{

final Business business=new Business();

new Thread(new Runnable()

{

public void run(){

for(int i=0;i<50;i++)

{

business.SubThread(i);

}

}

}

).start();

Thread.sleep(1000); //此行为了让主线程让出CPU,让子线程先执行

for(int i=0;i<50;i++)

{

business.MainThread(i);

}

}

class Business

{

//boolean bShouldSub=true;

public synchronized void MainThread(int i) throws InterruptedException

{

for(int j=0;j<100;j++)

{

System.out.println(Thread.currentThread().getName()+":yu_i="+i+",j="+j);

}

this.notify();

try{

this.wait();

}catch(InterruptedException e){

e.printStackTrace();

}

}

public synchronized void SubThread(int i)

{

for(int j=0;j<10;j++)

{

System.out.println(Thread.currentThread().getName()+":bing_i="+i+",j="+j);

}

this.notify();

try{

this.wait();

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐