I comprehend the basics of Java from the book by Herbert Shildt I decided to reproduce the example from the book:
public class Test { public static void main(String args[]) { Callme target = new Callme(); Caller obj1 = new Caller(target, "Welcome"); Caller obj2 = new Caller(target, "to synchronized"); Caller obj3 = new Caller(target, "world!"); try { obj1.t.join(); obj2.t.join(); obj3.t.join(); } catch(InterruptedException e) { System.out.println("interrupted!"); } } } class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Thread is interrupted!"); } System.out.println("]"); } } class Caller implements Runnable{ String msg; Callme target; Thread t; public Caller(Callme trg, String s) { target = trg; msg = s; t = new Thread(this); t.start(); } public void run() { synchronized(target) { target.call(msg); } } } Judging by the textbook, the conclusion should be as follows:
[Welcome] [to synchronized] [world!] Instead, we get the following:
[Welcome] [world!] [to synchronized] I apologize in advance for the possibly Nubian question, but I don’t see an error in the emphasis.
UPD:
Even if you put a delay between the creation of objects as follows:
Callme target = new Callme(); Caller obj1 = new Caller(target, "Welcome"); Caller obj2 = new Caller(target, "to synchronized"); Thread.sleep(700); Caller obj3 = new Caller(target, "world!"); it still fails to achieve the desired effect. As for me it is so very strange.