Hi all
I tried compiling and running this question and answer in my
Java environment but got a different answer to the answers in the book. The question was on Threads i.e.
public class Messager implements Runnable {
public Messager(
String name) { this.name = name;}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new
Thread(new Messager("Wallace")).start();
new Thread(new Messager("Gromit")).start();
}
private String name;
private synchronized void message(int n) {
System.out.println("\n name - " + name + " " + n);
}
public void run() {
message(1);
//System.out.println("\n One Thread called" );
message(2);
}
}
The answer I got was
Wallace - 1
Wallace - 2
Gromit - 1
Gromit - 2
The answer in the book was
Wallace - 1
Gromit - 1
Gromit - 2
Wallace -2
Can anyone assist or had the same problem too?
Bola