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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -