为什么这种同步方法不能按预期工作?

时间:2015-11-03 21:39:00

标签: java multithreading synchronization

我有一个名为MyRunnable的课程:

public class MyRunnable extends Main implements Runnable  {
  String name; // name of thread
  Thread t;

  MyRunnable (String threadname) {
      name = threadname;
      t = new Thread(this, name);
      t.start();
  }

  public void run() {
      try {
          for (int i=0;i<100000;i++) {
              extend(1);
          }
      } catch (InterruptedException e) {
          System.out.println("Thread interrupted.");
      }
      System.out.println("Thread " +  name + " exiting.");
  }

}

和一个名为Main的类:

public class Main {
  private static List<Integer> numbers=new ArrayList<>();

  public synchronized void extend (int i) throws InterruptedException {
      numbers.add(i);
  }
  public synchronized static int getSize() {
      return numbers.size();
  }

  public static void main(String[] args) {
      MyRunnable t0=new MyRunnable("0");
      MyRunnable t1=new MyRunnable("1");
      MyRunnable t2=new MyRunnable("2");

      try {
          t0.t.join();
          t1.t.join();
          t2.t.join();
      } catch (InterruptedException e) {

      }
      System.out.println(getSize());
  }
}

现在我希望得到300000作为输出,但我得到一个随机数(大约在250000和290000之间),即使我确实使用了同步方法。我确实阅读了oracle的文档http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html,但我似乎无法弄清楚为什么这不能按预期工作。有人能解释我为什么吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

synchronized这里锁定了调用方法extend的对象(因为它是一个实例方法)。因此,您要在三个不同的对象上进行同步。

如果您在共享的static列表上进行同步(例如),您将获得预期的结果。

答案 1 :(得分:2)

方法与调用它们的对象同步。您需要创建一个在每个对象之间共享的对象,并让它们在该对象上同步。

private static List<Integer> numbers=new ArrayList<>();
public synchronized void extend (int i) throws InterruptedException {
    synchronize(numbers) {
        numbers.add(i);
    }
}
相关问题