这几天郁闷了,一直想不通为什么 ,明明加了同步啊为什么还会打印出下面那情况:
1..0.....1....
1..0.....2....
1..0.....3....
1..0.....3....

1..0.....4....
1..0.....5....
1..0.....6....
1..0.....6....

1..0.....7....
1..0.....8....class Myexe extends Thread {
boolean flag;
int shdown;
static int num = 0; Myexe() {
this.flag = false;
this.shdown = 0;
} Myexe(boolean flag) {
this.flag = flag;
this.shdown = 0;
} public void shutdownn() {
this.shdown = 1;
} public static void main(String[] args) throws Exception {
Myexe[] arrp = new Myexe[1];
Myexe[] arrc = new Myexe[1]; for (Myexe p : arrp) {
p = new Myexe(true);
p.start();
} for (Myexe c : arrc) {
c = new Myexe();
c.start();
} return; } public void run() {
if (this.flag) {
while (this.shdown == 0) { synchronized (this) {
System.out.println("1.." + this.shdown + "....." + (++num) + "....");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
}
} else { while (this.shdown == 0) {
synchronized (this) {
System.out.println("1.." + this.shdown + "....." + (++num) + "....");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
}
}