// 面试题,子线程10次与主线程100次来回循环执行50次 public class interview { public static void main(String[] args) { final Business b = new Business(); new Thread(new Runnable(){ @Override public void run() { for (int i = 1; i <= 50; i++) { b.sub(i); } } }).start(); for (int i = 1; i <= 50; i++) { b.main(i); } } } class Business{ private boolean flag; public synchronized void sub(int i){ while (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 10; j++) { System.out.println("sub Thread is sequence of "+j+"..lamp of "+i); } System.out.println("/.................."); flag = true; notify(); } public synchronized void main(int i){ while (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int j = 1; j <= 100; j++) { System.out.println("main Thread is sequence of "+j+"..lamp of "+i); } flag = false; System.out.println("........................"); notify(); } }
面试题,子线程10次与主线程100次来回循环执行50次
原创
©著作权归作者所有:来自51CTO博客作者倁之的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
python循环100次代码 python 循环10次
一、while循环的基础语法语法 注意 案例练习 i = 1num = 0while i < 101: num = num + i i = i + 1print(num) import randomnum = random.randint(1, 100)flag = Truecount = 0while fla
python循环100次代码 python 开发语言 Powered by 金山文档 for循环