0

In a pice of code in which a class start a thread calling start method.

it throw an illegalstate exception. But if i call run() it gose well.

Can you expain me why ?

class A{


void methodA(){
   T t = new T();
    t.start();     // illegal state exception
    t.run();  ///ok 
}

}

class T extends Thread{

....

....

}

real 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 <host> <port> | 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);
}
  }
 }

class extends thread:

  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<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<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") > 0) && (received.indexOf("ASK") > 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());
        }
    }
}
  }
FrankTan
  • 1,626
  • 6
  • 28
  • 63

4 Answers4

8

Well, if you call run(), you're just calling a method in your current thread. With start() you're attempting to start the Thread, and if it has for example already been started (or it has already stopped), you'll get an IllegalStateException.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

ummm... you have already started the WDAgent Thread at constructor, so when you are trying to start again it thorws IllegalStateException.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

You wanted to start it here:

...
try {
    WDAgent m_agent = new WDAgent(agent_address, Integer.parseInt(agent_port));
    m_agent.run(); // <-- Run that you wanted to be a start
} catch (NumberFormatException e1) {
...

But it was started in the constructor:

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(); // <<----- It was already started here!!!!
}

You are running the run method twice, once in the new thread (you start it in the constructor) and one in the main thread by calling run.

aalku
  • 2,860
  • 2
  • 23
  • 44
0

t.start() is used to start the thread's execution. t.start() is used one time only. If you want to a run the thread again you will need to use t.run().