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);
}
}
}
}}

解决方案 »

  1.   

    Usage1 类里 不好定义Thread1 这个类的。
    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);
    }
    }
    }
    }
      

  2.   

    就是我发的程序在ECLIPSE里调试出错,但我不知道错在那里了,请高手帮忙改一下
      

  3.   

    一楼正解。
    你把thread1类定义成内部类了。。放外面就好了。。