multithreading - Java thread join 3 -


the program creates thread t0 spawns thread t1 , subsequently threads t2 , t3 created.after execution of thread t3and application never returns other threads spawned earlier(t0,t1,t2) , left stuck.

why threads t0, t1, , t2 suspended?

     public class cult extends thread              {                   private string[] names = {"t1", "t2", "t3"};                 static int count = 0;                 public void run()                  {                     for(int = 0; < 100; i++)                      {                         if(i == 5 && count < 3)                          {                             thread t = new cult(names[count++]);                             t.start();                             try{                                 thread.currentthread().join();                             }                             catch(interruptedexception e)                             {                                 e.printstacktrace();                             }                         }                         system.out.print(thread.currentthread().getname() + " ");                     }                   }                   public static void main(string[] a`)                  {                      new cult("t0").start();                  }             } 

the important point missed:

thread.currentthread().join(); 

method join in source code uses isalive method.

public final synchronized void join(long millis)      ...     if (millis == 0) {         while (isalive()) {         wait(0);         }     ...     } 

it means thread.currentthread().join() return when thread.currentthread() dead.

but in case it's impossible because of running code in thread.currentthread() has peace of code thread.currentthread().join(). that's why after thread 3 completion program should hang , nothing happens thereafter.

enter image description here


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -