-2
    public class Messager implements Runnable {
      public static void main(String[] args) {
        new Thread(new Messager("Wallace")).start();
        new Thread(new Messager("Gromit")).start();
    }
     private String name;
     public Messager(String name) {
         this.name = name;
     }
     public void run() {
      message(1);
      message(2);
    }
     private synchronized void message(int n) {
      System.out.print(name + "-" + n + " ");
     }
  }

is : B. Wallace-1 Gromit-2 Wallace-2 Gromit-1 a possible result of the execution of this code?

4

1 回答 1

1

不,这不是可能的结果。

message(1)并按message(2)顺序执行。

所以,Gromit-1后面一定要跟Gromit-2

于 2015-06-09T03:04:43.307 回答