?
public class Test {
?private Object obj=new Object();
?boolean flg;
?
?public static void main(String[] args) {
??
??Test t=new Test();
??ThreadA a=t.new ThreadA();
??ThreadB b=t.new ThreadB();
??a.start();
??b.start();
??
?}
?
?
?class ThreadA extends Thread{
??public void run() {
???while(true){
???synchronized(obj){
????try {
?????if(flg){
??????obj.wait();
?????}
?????
????} catch (InterruptedException e) {
?????e.printStackTrace();
????}
????System.out.println("a");
????flg=true;
????obj.notify();
???}
??}
??}
??
?}
?
?class ThreadB extends Thread{
??public void run() {
???while(true){
???synchronized(obj){
????try {
?????if(!flg){
??????obj.wait();
?????}
????} catch (InterruptedException e) {
?????e.printStackTrace();
????}
????System.out.println("b");
????flg=false;
????obj.notify();
???}
??}
??}
?}
}