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?
No, it is not a possible result.
message(1)
andmessage(2)
are executed in the order.So,
Gromit-1
must be followed byGromit-2
.