java - How does a thread know that there is a join method ahead -


below sample code, when a.start() called should create thread , print "run" immediately. why called after printing "begin" 20 times.

how thread "a" decide doesn't have call run() immediately.

public class jointest implements runnable {      public static void main(string[] args) throws interruptedexception {         thread = new thread(new jointest());         a.start();          (int = 0; < 20; i++) {             system.out.print("begin");         }         thread.sleep(1000);          a.join();         system.out.print("\nend");     }      public void run() {         system.out.print("\nrun");     } } 

output:

beginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbeginbegin run end 

i little confused behavior of thread.

in opinion "run" should printed before "begin" because printed before join() method called, , @ time of join method called thread "a" must have finished execution , calling join must useless @ point.

calling start() on thread doesn't triggers execution of run() method immediately. thread marked started main thread pursues execution for loop. jvm switching thread once main thread reaches sleep() statement.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -