使用GreenRobot EventBus从线程到线程进行通信

时间:2013-11-13 10:37:07

标签: java android multithreading queue greenrobot-eventbus

刚开始使用GreenRobot的EventBus

只有一件事让我苦苦挣扎:你如何在不同的线程之间进行通信,以便onEventXY()方法实际上也在订阅的线程中执行。

看来,当您发布事件时,订阅者onEvent方法是从与发布事件的线程相同的线程中调用的。那不是我想要的。

我希望能够使用EventBus进行线程间通信,使得实际接收的事件对象由接收事件对象的线程中的onEvent方法处理。

这可能吗?

示例:

  • mainThread
  • backGroundThread1
  • backGroundThread2

MainThread在EventBus上发布事件,backGroundThread1在其onEventXY()方法中接收它并在其自己的线程中执行代码(设置一些变量),backGroundThread2在其onEventXY方法中接收它并在其自己的线程中执行代码(设置一些变量)。

如果这还不可能,我注定要使用线程队列,比如BlockingQueue,这很难实现。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

来自https://github.com/greenrobot/EventBus

的Greenrobot文档
  

<强> BackgroundThread
  订阅者将在后台线程中调用。如果发布线程不是主线程,则将在发布线程中直接调用事件处理程序方法。如果发布线程是主线程,则EventBus使用单个后台线程,该线程将按顺序传递其所有事件。使用此模式的事件处理程序应尝试快速返回以避免阻塞后台线程。

     

Async
事件处理程序方法在单独的线程中调用。这始终独立于发布线程和主线程。发布事件永远不会等待使用此模式的事件处理程序方法。如果执行可能需要一些时间,例如事件处理程序方法应使用此模式。用于网络访问。避免同时触发大量长时间运行的异步处理程序方法来限制并发线程数。 EventBus使用线程池从已完成的异步事件处理程序通知中有效地重用线程。

创建回调时,需要将名称后缀添加到onEvent,如下所示:

  • onEventMainThread(YourEvent eventInstance)和简写onEvent(YourEvent eventInstance)
    始终调度到主UI线程
  • onEventBackgroundThread(YourEvent eventInstance)这最适合您的问题
  • onEventAsync(YourEvent eventInstance)
    总是在一个新线程中,如果你从这里发送大量事件有风险,可以轻松点击线程异常

答案 1 :(得分:3)

我遇到了同样的问题,我使用了LooperHandlerHandlerThread

这是我的BackgroundHandlerThread类:

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Process;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.UUID;


public class BackgroundHandlerThread extends Handler {

    private static final String TAG = BackgroundHandlerThread.class.getSimpleName();

    private HandlerThread handlerThread;
    private Object busHandler;

    public BackgroundHandlerThread(HandlerThread handlerThread, Object busHandler) {
        super(handlerThread.getLooper());
        this.handlerThread = handlerThread;
        this.busHandler = busHandler;
    }

    public void onEvent(Object event) {
        Log.d(TAG, "onEvent(Object), thread: " + Thread.currentThread().getId() + ", class: " + event.getClass().getName());
        Message message = obtainMessage();
        message.obj = event;
        sendMessage(message);
    }

    @Override
    public void handleMessage(Message msg) {
        Method[] aClassMethods = busHandler.getClass().getDeclaredMethods();
        for (Method m : aClassMethods) {
            if (m.getName().equals("onHandlerThreadEvent")) {
                if (m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(msg.obj.getClass())) {
                    try {
                        m.invoke(busHandler, msg.obj);
                    } catch (Exception e) {
                        Log.wtf(TAG, e);
                    }
                }
            }
        }
    }

    public boolean quit() {
        return handlerThread.quit();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public boolean quitSafely() {
        return handlerThread.quitSafely();
    }

    public static class Builder {
        private HandlerThread handlerThread;
        private Object busHandler;

        public Builder(Object busHandler) {
            this.busHandler = busHandler;
        }

        public Builder setHandlerThread(HandlerThread handlerThread) {
            this.handlerThread = handlerThread;
            return this;
        }

        public BackgroundHandlerThread build() {
            if (handlerThread == null) {
                handlerThread = new HandlerThread("BackgroundHandlerThread: " + UUID.randomUUID().toString(), Process.THREAD_PRIORITY_BACKGROUND);
            }

            if (!handlerThread.isAlive()) {
                handlerThread.start();
            }

            return new BackgroundHandlerThread(handlerThread, busHandler);
        }
    }
}

我在我的服务中使用它,但BackgroundHandlerThread对象可以绑定到任何对象。

import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import de.greenrobot.event.EventBus;

public class DeviceService extends Service {

    private static final String TAG = DeviceService.class.getSimpleName();

    private BluetoothDevice bluetoothDevice;
    private BackgroundHandlerThread handlerThread;
    private boolean connected = false;

    //region Lifecycle
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate, thread: " + Thread.currentThread().getId());
        handlerThread = new BackgroundHandlerThread.Builder(this).build();
        EventBus.getDefault().register(handlerThread);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(handlerThread);
        handlerThread.quit();
        super.onDestroy();
    }
    //endregion

    public void onHandlerThreadEvent(ConnectToDeviceEvent event) {
        Log.d(TAG, "onHandlerThreadEvent, thread: " + Thread.currentThread().getId());
        connected = true;
        bluetoothDevice = event.device;
        EventBus.getDefault().post(new ConnectionStateChangedEvent(bluetoothDevice, connected));
    }

    //region Static manipulation
    public static void startService(Context context) {
        Intent intent = new Intent(context, DeviceBinder.class);
        context.startService(intent);
    }
    //endregion
}

活动类:

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;

import de.greenrobot.event.EventBus;

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onStartClick, thread: " + Thread.currentThread().getId());
                EventBus.getDefault().post(new ConnectToDeviceEvent(application.getCurrentStateProvider().getDevice()));
            }
        });
        DeviceService.startService(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    public void onEventMainThread(ConnectionStateChangedEvent event) {
        Log.d(TAG, "onEventMainThread(ConnectionStateChangedEvent), thread: " + Thread.currentThread().getId());
    }

}

日志输出:

D/MainActivity: onStartClick, thread: 1
D/BackgroundHandlerThread: onEvent(Object), thread: 1, class: ConnectToDeviceEvent
D/DeviceService: onHandlerThreadEvent, thread: 4399
D/BackgroundHandlerThread: onEvent(Object), thread: 4399, class: ConnectionStateChangedEvent
D/MainActivity: onEventMainThread(ConnectionStateChangedEvent), thread: 1

类似:Best practice for eventbus with thread safety