package com.cnxieyang.traditionalcommunication
;
public class ThreadCommunication {
// 创建了线程
public static void main(String[] args) {
final Communicaion comm = new ThreadCommunication().new Communicaion();
new Thread(new Runnable() {
@Override
public void run() {
// 在该内部实现子线程的循环的10次,外部的循环保证实现了50次
for (int i = 1; i <= 50; i++) {
// 循坏100次
comm.sub(i);
}
}
}).start();
for (int i = 1; i <= 50; i++) {
// 循环100次
comm.main(i);
}
}
class Communicaion {
boolean temp = true;
public synchronized void sub(int i) {
while (!temp) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 10; j++) {
System.out.println("sub" + j + "is " + i);
}
temp=false;
this.notify();
}
public synchronized void main(int i) {
while(temp){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int j = 1; j <= 100; j++) {
System.out.println("main" + j + "is " + i);
}
temp=true;
this.notify();
}
}
}