title: Use of thread in Java class without implementing Runnable nor extending Thread
body: <p>I'm writing a class SipProvider that could receive and send datagramPacket. This class is part of a SIP Stack i'm working on.</p>
  
  <p>The API i have been provided doesn't make SipProvider Implement Runnable or Extend Thread.</p>
  
  <p>The solution I'm trying to implement is to create a new class inside SipProvider which will extend thread. I want to give argument to the thread and i have a problem of compilation (No enclosing instance of type SipProvider is accessible), the thread cannot be instantiated because it is related to a SipProvider (i found it should be static but didn't know how to do it).</p>
  
  <p>I've looked up the Internet on how implementing a thread inside a class but didn't find a solution. Is there a known way to do it. </p>
  
  <p>this is a snapshot of what i've been trying to do. This is only a part of the class. </p>
  
  <pre><code>public class SipProvider {
  
      //startOn is the method which allow the user to listen on a port 
      //so the user don't have to bother creating a thread and so on
  public static SipProvider startOn(listeningPoint) {
          SipProvider sipProvider = new SipProvider();
          thread.sipProvider = sipProvider;
          thread.run();
          return sipProvider; 
  }
  
      //this is the thread i want to handle the listening process 
  public class ReceiveThread extends Thread{
  
      public SipProvider sipProvider;
  
      public ReceiveThread(SipProvider sipProvider){
          this.sipProvider = sipProvider;
      }
  
      @Override
      public void run(){
          try {
  
              int MAX_LEN = 200;
              DatagramSocket datagramSocket = new DatagramSocket(
                      listeningPoint.getPort());
              sipProvider.datagramSocket = datagramSocket;
              byte[] buffer = new byte[MAX_LEN];
              DatagramPacket packet = new DatagramPacket(buffer, MAX_LEN);
              while (!datagramSocket.isClosed()) {
                  sipProvider.setSipListener(sipListener);
                  datagramSocket.receive(packet);
                                      //handle packet content
  
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  }
  </code></pre>
  
  <p><strong>EDITH:</strong></p>
  
  <p>One other solution i found besides the one proposed (which works perfectly) is the instantiate a new thread and define its run() method at the same time. the method startOn in my example starts the listening on a listeningPoint (which contains information about port address and protocol used). </p>
  
  <pre><code>public static SipProvider startOn(final ListeningPoint listeningPoint) throws   SocketException {
      final SipProvider sipProvider = new SipProvider(listeningPoint);
  
      new Thread() {
          @Override
          public void run() {
              try {
  
                  while (true) {
  DatagramPacket packet = newDatagramPacket(new byte[200], 200);
  sipProvider.datagramSocket.receive(packet);
  String content = new String(packet.getData(), 0, packet.getLength());
  sipProvider.sipListener.processContent(content);
                  }
  
              } catch (Exception e) {
              }
          }
      }.start();
      return sipProvider;
  } 
  </code></pre>