Use of thread in Java class without implementing Runnable nor extending Thread

I'm writing a class SipProvider that could receive and send datagramPacket. This class is part of a SIP Stack i'm working on.

The API i have been provided doesn't make SipProvider Implement Runnable or Extend Thread.

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).

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.

this is a snapshot of what i've been trying to do. This is only a part of the class.

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();
        }
    }
}
}

EDITH:

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).

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;
} 

This code snippet may help you:

public class SipJob implements Runnable {

    private SipProvider sipProvider;

    public SipJob(SipProvider sipProvider) {
                  // pass other parameters, if you need
        this.sipProvider=sipProvider;
    }

    @Override
    public void run() {
        // do the code, use SipProvider

    }
}

This will contain the code you want to run. You can add as many parameters to the constructor as you want, you can use them from your business logic. I recommend not to pass resources (eg sockets), because they must be closed, and it is not wise to separate the opening and closing code (the same entity must be responsible for both).

Now you need a Thread to execute:

SipProvider sipProvider = ...;
Runnable job = new SipJob(sipProvider);
Thread runner = new Thread(job);
runner.setDaemon(true);
runner.start();

Notice that - as far as I understood - this will run in the background, so I set setDaemon(true) .

I'm not sure, but you may have internal stuff in the SipProvider what you want to use in the thread. This way I recommend first to extend the SipProvider class, enabling access to those internal variables/methods, the use the child class in the SipJob class.

EDIT: Another way to access the internals of SipProvider is to extend the class, and to define the SipJob class as an inner class (not inner static class, but rather instance class) in the descendant. This way you will automatically have access to the instance variables of the containing SipProvider - you even don't have to pass it as an argument. Try this from the inner instance class: SipProvider.this.whateverField .

链接地址: http://www.djcxy.com/p/92114.html

上一篇: 在java中给出了默认的新线程名称?

下一篇: 在Java类中使用线程而不实现Runnable,也不扩展Thread