package cn.test.thread;
/*
* 子线程10次,主线程100次,来回50次
*/
public class ThreadTest1 {
public static void main(String[] args) throws InterruptedException {
ThreadTest1 tt = new ThreadTest1();
tt.init();
}
public void init() throws InterruptedException {
final Business bussiness = new Business();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
bussiness.subThread();
}
}
}).start();
Thread.sleep(1000); //此行为了让主线程让出CPU,让子线程先执行
for (int i = 0; i < 50; i++) {
bussiness.mainThread();
}
}
class Business {
public synchronized void mainThread() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" execute"+i);
}
this.notify();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void subThread() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+" execute"+i);
}
this.notify();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
子线程循环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循环