1 Reply Latest reply on Jul 16, 2013 4:28 PM by jamezp

    JBoss 7.1.1 Thread Sleep

    execretor

      Hi guys,

       

      There is a problem with thread management.

      I have a custom jar module, loaded by ear application. In this jar I create some threads and, in every thread, i must read from socket some informations.

      When i read data, i create a new thread with a timeout. If my read exceed this timeout, i must exit from read and log an exception. But, if i do a Thread.Sleep

      into this new wait thread, this stop also the main thread.

       

       

      How can i do a simple thread? And how can i sleep it?

       

       

       

      This is my simple code:

       

      package atlas.core.protocols.util;

       

      import java.io.IOException;

      import java.io.InputStream;

      import java.io.OutputStream;

      import java.net.Socket;

       

      public class SocketManager implements Runnable {

      public static byte[] recv(Socket sk, InputStream in, int maxLen) throws IOException {

      SocketManager sockMan = new SocketManager(in, sk);

      Thread th = new Thread(sockMan);

      th.run();

       

      byte[] res = _recv(in, maxLen);

       

      if(res != null) {

      sockMan.setOk(true);

      return res;

      } else {

      return null;

      }

      }

       

      private static byte[] _recv(InputStream in, int maxLen) throws IOException {

      byte[] res = new byte[maxLen];

      int n = 0;

      int len = 0;

       

      while((n = in.read(res, len, maxLen - len)) > -1 && len < maxLen) {

      len += n;

      }

       

      if(len == maxLen) {

      return res;

      } else {

      return null;

      }

      }

       

      public static boolean send(OutputStream out, byte[] output) {

      try {

      out.write(output);

      return true;

      } catch (IOException e) {

      e.printStackTrace();

      return false;

      }

      }

       

      private InputStream inChannel;

      private Socket socket;

      private boolean isOk;

       

      public SocketManager(InputStream in, Socket sk) {

      inChannel = in;

      socket = sk;

      }

       

      @Override

      public void run() {

      try {

      Thread.sleep(10000);

       

      if(!isOk) {

      inChannel.close();

      socket.close();

      }

      } catch (InterruptedException | IOException e) {

      e.printStackTrace();

      }

      }

       

       

      public boolean isOk() {

      return isOk;

      }

       

      public void setOk(boolean isOk) {

      this.isOk = isOk;

      }

      }

       

       

      Thanks.

        • 1. Re: JBoss 7.1.1 Thread Sleep
          jamezp

          Well really you shouldn't be launching threads in EE applications. That said you're executing Thread.run() which blocks instead of Thread.start(). The SocketManager also isn't thread-safe

           

          --

          James R. Perkins