java学习

java学习

 

子线程执行10次,主线程执行100次,然后再子线程执行10次,主线程执行100次,循环50次

package com.yjw.thread;
public class Test2 {
static class Businis {
boolean f = false;// true sub执行,false main执行
public synchronized void sub(int i) {
if (!f) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("sub=" + j + ",loop=" + i);
}
f = false;
this.notify();
}
public synchronized void main(int i) {
if (f) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 100; j++) {
System.out.println("main=" + j + ",loop=" + i);
}
f = true;
this.notify();
}
}
public static void main(String[] args) {
final Businis businis = new Businis();
for (int i = 1; i <= 50; i++) {
businis.main(i);
                        businis.sub(i);
}
}
}

posted on 2017-12-08 16:24 杨军威 阅读(238) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜