繁体   English   中英

如何从另一个线程调用线程的getHandler()方法?

[英]How can I call a thread's getHandler() method from another thread?

我遇到了一个问题,即一个线程在初始化其处理程序之前尝试将消息发送到另一个线程的处理程序。 这种异步线程通信很容易导致nullpointerexception。

我正在尝试使用以下代码来解决此问题(一种等待通知算法),但是我不明白如何从发送消息的线程中调用getHandler(),因为我一直在获取“无法从静态上下文”错误。

尝试修复消息接收线程的代码:

public class LooperThread extends Thread {

    private static Handler mHandler;

    public void run() {
        Looper.prepare();

        synchronized (this) {
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    // process incoming messages here
                }
            };
            notifyAll();
        }

        Looper.loop();
    }

    public synchronized Handler getHandler() {
        while (mHandler == null) {
            try {
                wait();
            } catch (InterruptedException e) {
                //Ignore and try again.
            }
        }
        return mHandler;
    }
}

当我尝试以下代码时,我不断收到“无法从静态上下文编译器错误调用非静态方法的信息。

消息发送线程:

public class SenderThread extends thread{
    private static Handler senderHandler;

    public void run(){
        Looper.prepare();

        senderHandler = LooperThread.getHandler(); //This is where the error occurs!

        //do stuff
        senderHandler.msg(obj);
        Looper.loop();
    }
}

我知道我可能不应该尝试在run()方法中初始化发送方线程的处理程序,因为它将被重复调用,因此很浪费。 我应该在哪里调用LooperThread的getHandler()方法?

背景信息:

我以这个问题和一个答案作为参考: 如何确保另一个线程的处理程序在调用前不为null?

错误的含义Non-static method cannot be called from a static context是因为您试图以静态方式使用非静态(类成员)(在您的示例中,引用LooperThread )。 解决方法通常是使方法在故障时是静态的,例如, public static synchronized Handler getHandler()

但是在您的情况下,您使用的是非静态方法wait() (因此无法从静态上下文中访问它)。 相反,您应该将mHandler更改为非静态状态(因此每个线程都会有一个mHandler这就是您想要的): private Handler mHandler;

SenderThread内部,您需要构造一个LooperThread ,然后可以调用其非静态的getHandler()

public class SenderThread extends Thread {
    private static Handler senderHandler;

    public void run(){
        Looper.prepare();

        LooperThread looperThread = new LooperThread();
        senderHandler = looperThread.getHandler(); // Should no longer error :-)

        //do stuff
        senderHandler.msg(obj);
        Looper.loop();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM