Possible execution result

287 views Asked by At
    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?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

No, it is not a possible result.

message(1) and message(2) are executed in the order.

So, Gromit-1 must be followed by Gromit-2.