title: Java thread illegal state exception
body: <p>In a pice of code in which a class start a thread calling start method.</p>
  
  <p>it throw an illegalstate exception. But if i call run() it gose well.</p>
  
  <p>Can you expain me why ? </p>
  
  <pre><code>class A{
  
  
  void methodA(){
     T t = new T();
      t.start();     // illegal state exception
      t.run();  ///ok 
  }
  
  }
  
  class T extends Thread{
  
  ....
  
  ....
  
  }
  </code></pre>
  
  <p>real code:</p>
  
  <pre><code>  public class FileMultiServer
   {
    private static Logger _logger = Logger.getLogger("FileMultiServer");
  
    public static void main(String[] args) 
    {
  
   ServerSocket serverSocket = null;
   boolean listening = true;
  
   String host = "localhost";
   int porta = 4444;
   InetSocketAddress addr = null;
   String archiveDir = System.getProperty("java.io.tmpdir");
  
   Aes aes = new Aes();
  
   Properties prop = new Properties();
   //load a properties file
   File propFile = new File("config.properties");
   try {
      System.out.println(propFile.getCanonicalPath());
       prop.load(new FileInputStream("config.properties"));
  
   } catch (Exception e1) {
      // TODO Auto-generated catch block
      System.out.println(e1);
   }
  
  
   DBConnector.dbName = prop.getProperty("database");
   DBConnector.ipDb = prop.getProperty("urlDb");
   DBConnector.dbPort = prop.getProperty("dbport");
   DBConnector.userName = prop.getProperty("dbuser");
   DBConnector.password = aes.DeCrypt(prop.getProperty("dbpassword"));         
  
   String agent_address = prop.getProperty("agent_ip");
   String agent_port = prop.getProperty("agent_port");
  
   try {
      WDAgent m_agent = new WDAgent(agent_address, Integer.parseInt(agent_port));
      m_agent.run();
  } catch (NumberFormatException e1) {
      // TODO Auto-generated catch block
      System.out.println("errore nell'agent");
      e1.printStackTrace();
  } catch (Exception e1) {
      // TODO Auto-generated catch block
      StringWriter errors = new StringWriter();
      e1.printStackTrace(new PrintWriter(errors));
      _logger.debug(e1.getMessage());
      _logger.debug(errors.toString());
      System.out.println(e1.getMessage());
      System.out.println(errors.toString());
  } 
  
  
  
   String logCfg = System.getProperty("LOG");
   if (logCfg != null) DOMConfigurator.configure(logCfg); else
     DOMConfigurator.configure("log4j.xml");
   try
   {
     if (args.length == 0) {
       host = "localhost";
       porta = 4444;
     }
     else if (args.length == 1) {
       porta = Integer.parseInt(args[0]);
     } else if (args.length == 2) {
       host = args[0];
       porta = Integer.parseInt(args[1]);
     } else if (args.length == 3) {
       host = args[0];
       porta = Integer.parseInt(args[1]);
       archiveDir = args[2];
     } else {
       _logger.info("usage: server &lt;host&gt; &lt;port&gt; | server [port] | server");
       System.exit(88);
     }
   } catch (Exception e) {
     _logger.error(e.getMessage());
     _logger.info("enter a numer for argument or nothing....");
     System.exit(88);
   }
  
   _logger.info("Allocating listen to: " + host + ":" + porta);
   try
   {
     addr = new InetSocketAddress(host, porta);
     serverSocket = new ServerSocket();
     serverSocket.bind(addr);
   } catch (Exception e) {
     _logger.error(e.getMessage());
     _logger.error("Could not listen on port: " + porta);
     System.exit(-1);
   }
  
   _logger.info("Server listening on " + host + "-" + addr.getAddress().getHostAddress() + ":" + porta);
   while (listening) {
     try {
      new FileMultiServerThread(serverSocket.accept(), archiveDir).start();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          System.out.println(e);
      }
   }
   try {
      serverSocket.close();
  } catch (Exception e) {
      // TODO Auto-generated catch block
      System.out.println(e);
  }
    }
   }
  </code></pre>
  
  <p>class extends thread:</p>
  
  <pre><code>  public class WDAgent extends Thread 
    {
  private  String _PID=null;
  // *************************************************************************
  // private - static final
  // *************************************************************************
  DatagramSocket socket;
  boolean m_shutdown = false;
  
  private static final Logger m_logger = Logger.getLogger(WDAgent.class);
  
  private String getPID() {
      try {
          String ppid=System.getProperty("pid");
          String match= "-Dpid="+ppid;
          Runtime runtime = Runtime.getRuntime();
          String cmd="/bin/ps -ef ";
          Process process = runtime.exec(cmd);
          InputStream is = process.getInputStream();
          InputStreamReader osr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(osr);
          String line;
          while ((line = br.readLine()) != null) {
               m_logger.info("line 0: "+line);    
               if(line.indexOf(match)!=-1 ) {
               m_logger.info("line: "+line);
               String[] cols=line.split(" ");
               int count=0;
               String val=null;
               for (int k=0; k&lt;cols.length; k++) {
                    if(cols[k].length()==0) continue;
                    count++;
                    if(count==3) {
                        // Good. I answerd the question
                        String pid =  val;
                        m_logger.debug("pid processo: " + pid);
                        return pid;
                       }
                    val=cols[k];
                }
              }
          }
      } 
      catch (Exception err) 
      {
          m_logger.error("Error retrieving PID....", err);
          err.printStackTrace();
      } 
  
      return null;
  }
  
  public WDAgent(String address, int port) throws IOException
  {
      InetSocketAddress isa = new InetSocketAddress(address, port);
      socket = new DatagramSocket(isa);
      m_logger.info("Agent started on (" + address + ":" + port + ")");
      m_logger.info("Waiting for WD connection.");
  
      this.start();
  }
  
  void Finalize() throws IOException 
  {
      m_logger.info("Closing Agent.");
      socket.close();
      m_logger.info("Agent Closed");
  }
  
  public void Shutdown()
  {
      m_shutdown = true;
  }
  
  public void run() 
  {
      byte[] buffer = new byte[1024];
      int i;
  
      while(!m_shutdown) 
      {
          try {
              String answer = "Agent PID=123456";
              for (i=0; i&lt;1024; i++)
                  buffer[i] = 0;
  
              DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
              socket.receive(packet);
              String received = new String(packet.getData(), 0, packet.getLength());
  
              InetAddress client = packet.getAddress();
              int client_port = packet.getPort();
              m_logger.info("Received " + received + " from " + client);
  
              if ((received.indexOf("PID") &gt; 0) &amp;&amp; (received.indexOf("ASK") &gt; 0))
              {
                  if(_PID==null) {
                      _PID=getPID();
                  }
                  if(_PID!=null) {
                       answer = "Agent PID=" + _PID;
                       m_logger.debug("risposta: " + answer);
                       DatagramPacket answ_packet = new DatagramPacket(answer.getBytes(), answer.getBytes().length, client, client_port);
                       socket.send(answ_packet);
                  } else {
                      m_logger.error("no PID per rispondere a watchdog .... sorry");
                  }
              }
              else
                  m_logger.warn("Command not recognized");
          } 
          catch(IOException e) 
          {
              m_logger.error(e.getMessage());
          }
          catch(Exception e) 
          {
              m_logger.error(e.getMessage());
          }
      }
  }
    }
  </code></pre>