java - Threads synchronising not working -
i trying implement synchronization using following code, not working expected.
class callme extends thread{ synchronized void call( ) { system.out.print("[" + "hello"); try { thread.sleep(1000); } catch(interruptedexception e) { system.out.println("interrupted"); } system.out.println("]"); } public void run() { call(); } } public class threads { static void main(string args[]) { callme target = new callme(); callme target2 = new callme(); target.start(); target2.start(); }}
the output should [hello][hello] [hello[hello]] not synchronized.
you code working fine imo , in fact there not synchronizing issue there,
why not?:
every object of class thread calling own call
method, , printing when should, in fact trying same resource, (the system.out.print) not synchronized.... totally correct behavior like
[hello[hello]]
in output.
Comments
Post a Comment