0

I have two methods in a non-ui thread(SomeThread), and triggering these methods via a handler(SomeHandler). I'm starting the thread and then sending messages via handler to be invoked methods of SomeThread.

The problem is, after invoking startWork method, handler never handle messages again, as a result i cant run stopWork method

Question : How i can run stopWork method ?

My thread

private static class SomeThread extends Thread {
    private boolean mWorking;
    private volatile SomeHandler mHandler;

    public SomeHandler getHandler(){
        return mHandler;
    }

    @Override
    public void run() {
        Looper.prepare();
        mHandler = new SomeHandler(this);
        Looper.loop();
    }

    private void startWork(){
        mWorking = true;
        while(mWorking){
            // Doing heavy job
        }
    }

    private void stopWork(){
        mWorking = false;
    }
}

My handler

private static class SomeHandler extends Handler {
    private static final int MSG_START = 0;
    private static final int MSG_STOP = 1;

    private final WeakReference<SomeThread> mThreadRef;
    public SomeHandler (SomeThread thread){
        mThreadRef = new WeakReference<>(thread);
    }

    public void sendPlay(){
        sendEmptyMessage(MSG_START);
    }

    public void sendStop(){
        sendEmptyMessage(MSG_STOP);
    }

    @Override
    public void handleMessage(Message msg) {
        SomeThread mThread = mThreadRef.get();
        if(mThread == null) return;

        switch (msg.what){
            case MSG_START:
                mThread.startWork();
                break;
            case MSG_STOP:
                mThread.stopWork();
                break;
        }
    }
}

Usage of them

SomeThread mThread = new SomeThread();
mThread.start();

// This is working
SomeHandler mHandler = mThread.getHandler();
mHandler.sendPlay();

// This is not working
SomeHandler mHandler = mThread.getHandler();
mHandler.sendStop();
blackkara
  • 4,900
  • 4
  • 28
  • 58
  • `mHandler = new mHandler(this);` what is it? where is `mHandler` class? and why so strange name? what exactly do you want to achieve? – pskink Mar 08 '16 at 11:29
  • Sorry, it just a typo. now i edited. – blackkara Mar 08 '16 at 11:32
  • 2
    ok, so what is `SomeThread` for? maybe you should use a `HandlerThread`? why to make a custom `Thread` if you can use it a `HandlerThread`? whats your goal actually? see [this](http://stackoverflow.com/a/25096981/2252830) for more info – pskink Mar 08 '16 at 11:33
  • Even i use Thread or HandlerThread it behaves same. So, not working again. – blackkara Mar 08 '16 at 11:36
  • the usage is like this https://github.com/google/grafika/blob/master/src/com/android/grafika/TextureFromCameraActivity.java#L478 – blackkara Mar 08 '16 at 11:37
  • even if your `Thread` has a looping `Looper` it doesn't mean that when you are in a tight loop (like in `startWork` method) you can send a `Message` to it and receive it right away: you have to make a "heavy work" in some background thread, otherwise a `Looper` has no way to get next `Message`, something like [this](http://pastebin.com/GwahJGRN) – pskink Mar 08 '16 at 11:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105682/discussion-between-blackkara-and-pskink). – blackkara Mar 08 '16 at 12:03

0 Answers0