while while(true)然后同步(object)在while循环中有什么区别...而另一种方式是同步(object)和while(true)whithin?它在逻辑上有什么不同?我发现输出没有区别。
public class H extends Thread {
String info = "";
public H (String info) {
this.info = info;
}
public synchronized void run () {
try {
while ( true ) {
System.out.println(info);
this.notify();
this.wait();
}
} catch ( Exception e ) {}
}
public static void main (String args []) {
new H("0").start();
new H("1").start();
}
}
public class X extends Thread {
private String info;
static Object o = new Object();
public X (String info) {
this.info = info;
}
public void run () {
while ( true ) {
synchronized ( o ) {
System.out.println(info);
try {
o.notify();
sleep(100);
o.wait(1);
} catch ( Exception e ) { }
}
}
}
public static void main (String args []) {
( new X("0") ).start();
( new X("1") ).start();
}
}