java - Put join on Main thread unexpected behaviour -


i wrote following code :

public class threaddemo implements runnable { private thread t ; private string threadname;   threaddemo(string threadname)  {       this.t = new thread(this,threadname);      t.start();  }  public void run() {     system.out.println("new thread has been started!!!" + t.getname());  } public  static void main(string args[]) { new threaddemo("thread-1");  thread t = thread.currentthread(); try {     t.join(); } catch (interruptedexception e) {     e.printstacktrace(); }  new threaddemo("thread-2");  }    } 

so have putted join method on main thread . when run ,its execution never end. why ? why main thread doesn't end ? why it's running infinite time.

the join() method waits thread call on finish. in code, calling join() on current thread - same thread calling from. main thread going wait finish. never happens, because it's waiting on itself...

you should not join main thread, thread started instead.

threaddemo demo = new threaddemo("thread-1");  try {     demo.t.join(); } catch (interruptedexception e) {     e.printstacktrace(); } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -