日期:2014-05-20  浏览次数:20768 次

java 同步代码 很奇怪的现象

public class PrintTest{
static int index;
public  PrintTest()
{
index=0;
}
public  synchronized void printA(){
if(index%3==0)
{
index++;
System.out.print("A");
notifyAll();
}else{
try
{
wait();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}

}

public  synchronized void printB(){
if(index%3==1)
{

index++;
System.out.print("B");
notifyAll();
}else{
try
{
wait();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}

}


public  synchronized void printC(){
if(index%3==2)
{
index++;
System.out.print("C");
notifyAll();
}else{
try
{
wait();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}

}

public static  void main(String[]s)
{
PrintTest pt=new PrintTest();
PrintA pa=new PrintA(pt);
PrintB pb=new PrintB(pt);
PrintC pc=new PrintC(pt);
pa.start();
pb.start();
pc.start();
}

}

class PrintA extends Thread
{

PrintTest pt;
public PrintA(PrintTest pt)
{
this.pt=pt;
}
@Override
public void run() {
for(int i=0;i<10;i++)
{
pt.printA();
}
}

}


class PrintB extends Thread
{

PrintTest pt;
public PrintB(PrintTest pt)
{
this.pt=pt;
}
@Override
public void run() {
for(int i=0;i<10;i++)
{
pt.printB();
}
}

}

class PrintC extends Thread
{

PrintTest pt;
public PrintC(PrintTest pt)
{
this.pt=pt;
}
@Override
public void run() {
for(int i=0;i<10;i++)
{
pt.printC();
}
}

}

这是完整的代码,意图是ABCABC.......重复十次打印,可结果为ABCABCABCABCABC五次,为什么啊?
------解决方案--------------------
public class PrintTest
{
static int index;
static int count = 0;

public PrintTest()
{
index = 0;
}

public synchronized void printA()
{
if (index % 3 == 0)
{
index++;
System.out.print("A");
count++;
notifyAll();
} else
{
try
{
wait();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}

}

public synchronized void printB()
{
if (index % 3 == 1)
{

index++;
System.out.print("B");
count++;
notifyAll();
} else
{
try
{
wait();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}

}

public synchronized void printC()
{
if (index % 3 == 2)
{
index++;
System.out.print("C");
count++;
notifyAll();
} else
{
try
{
wait();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}

}

public static void main(String[] s)
{
PrintTest pt = new PrintTest();
PrintA pa = new PrintA(pt);
PrintB pb = new PrintB(pt);
PrintC pc = new PrintC(pt);
pa.start();
pb.start();
pc.start();
}

}

class PrintA extends Thread
{

PrintTest pt;

public PrintA(PrintTest pt)
{
this.pt = pt;
}

@Override
public void run()
{
while(pt.count < 30)
{
pt.printA();
}

// for (int i = 0; i < 10; i++)
// {
// pt.printA();
// }
}

}

class PrintB extends Thread
{

PrintTest pt;

public PrintB(PrintTest pt)
{
this.pt = pt;
}

@Override
public void run()
{
while(pt.count < 30)
{
pt.printB();

}

// for (int i = 0; i < 10; i++)
// {
// pt.printB();
// }
}

}

class PrintC extends Thread
{

PrintTest pt;

public PrintC(PrintTest pt)
{
this.pt = pt;
}

@Override
public void run()
{
while(pt.count < 30)
{
pt.printC();

}

// for (int i = 0; i < 10; i++)