这个和某个方法是不是static没有必然联系,要看这个方法有没有对某个字段(也就是非局部变量)进行读写操作。如果这个字段会在不同线程中被读写(可能是不同线程中的同一个方法,也可能不是同一个方法),那么就应该同步。

解决方案 »

  1.   

    要看这个方法有没有对某个字段(也就是非局部变量)进行读写操作。?我觉得如果这个字段如果不是static型的,各个线程实例中都会有它的对象,根本不会发生同步问题吧.
      

  2.   

    To usaspy(行走的鱼): public class Test {
      int x;  static void a(Test a) {
        a.x++;
      }  int getX() {
        return x;
      }  public static void main(String[] args) {
        final Test a = new Test();
        for (int i = 0; i < 10; i++) {
          new Thread("Thread " + i) {
            public void run() {
              while (true) {
                System.out.println(getName() + " Before " + a.getX());
                Test.a(a);
                System.out.println(getName() + " After " + a.getX());
              }
            }
          }.start();
        }
      }
    }
      

  3.   

    上面的代码就展示了多个线程争相读写同一个非static字段(int x)的情况