问题:
求解运行过程?
子线程循环10次,接着主线程循环5次,接着又回到子线程循环10次,主线程循环5次,这样总共循环50次
package com.huawei.interview;
/**
* 子线程循环10次,接着主线程循环5次,接着又回到子线程循环10次,主线程循环5次,这样总共循环50次
* @author yangjianzhou
*
*/
public class ThreadTest2 {
public static void main(String[] args) {
new ThreadTest2().init();
}
public void init(){
final Business business = new Business();
new Thread(
new Runnable(){
public void run(){
for(int i=0;i<50;i++){
business.SubThread(i);
}
}
}
).start();
for(int i=0;i<50;i++){
business.MainThread(i);
}
}
private class Business{
boolean bShouldSub = true;
public synchronized void MainThread(int i){
if(bShouldSub){
try{
this.wait();
}catch (Exception e) {
e.printStackTrace();
}
}
for(int j=0;j<5;j++){
System.out.println(Thread.currentThread().getName()+": i = "+i+", j = "+j);
}
bShouldSub=true;
this.notify();
}
public synchronized void SubThread(int i){
if(!bShouldSub){
try{
this.wait();
}catch (Exception e) {
e.printStackTrace();
}
}
for(int j=0;j<10;j++){
System.out.println(Thread.currentThread().getName()+" : i = "+i+", j = "+j);
}
bShouldSub = false;
this.notify();
}
}
}
求解运行过程?