要求主线程执行100次,子线程执行10次,主线程执行100次,子线程执行10次。。。循环
代码如下:
public class Test_02 {
public static void main(String[] args){
MeThread me = new MeThread();
Thread t = new Thread(me);
t.start();
boolean flag = true;
int i = 1;
synchronized (me) {
while (flag) {
System.out.println(Thread.currentThread().getName() + i);
i++;
if (i % 100 == 0) {
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("===========");
me.notify();//Thread.currentThread().notifyAll();也不行
}
}
}
}
}
}
class MeThread implements Runnable {
@Override
public synchronized void run() {
int i = 0;
boolean flag = true;
while (flag) {
System.out.println(Thread.currentThread().getName() + i);
i++;
if (i % 10 == 0) {
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("===========");
this.notify();//Thread.currentThread().notifyAll();也不行
}
}
}
}
}
蓝山帝景
炎炎设计
相关分类