public class Usage1 {
public static void main(String[] args) {
Thread1 th = new Thread1();
Thread th1 = new Thread(th, "A");
Thread th2 = new Thread(th, "B");
th1.start();
th2.start();
}class Thread1 implements Runnable {
public void run() {
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ " synchronized loop " + i);
}
}
}
}}
public class Usage {
public static void main(String[] args) {
Thread1 th = new Thread1();
Thread th1 = new Thread(th, "A");
Thread th2 = new Thread(th, "B");
th1.start();
th2.start();
}
} class Thread1 implements Runnable {
public void run() {
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ " synchronized loop " + i);
}
}
}
}
你把thread1类定义成内部类了。。放外面就好了。。