Search code examples
javamultithreadingguavaevent-bus

Java multithreading with Guava EventBus


I am using guava event bus. I have a server-like object that is supposed to be running all the time, listening for events to be posted to the b us. So in a junit test (MyObject is class under test), I create it in its own thread to simulate this and prevent blocking:

  @Test    
  public void test() {
      EventBus eventBus = new EventBus();

      Thread thread= new Thread() {
         @Override
         public void run()
         {
            logger.debug("Creating new thread");
            MyObject myObject = new MyObject(eventBus);
         }
      };

      thread.start();
      ...
  }

All is good, myObject is created off in its own thread, Thread1. Then later, I post an event to the event bus in my test:

eventBus.post(triggerObject);

The weird thing I am finding is that all the actions/logging from my subscribed method inside of MyObject class are executing in the main thread again. myObject waits for responses from certain other parts, and this blocks my test since it's in the main thread. Why is this happening? Is it something I am doing wrong with the EventBus or Java threading?


Solution

  • Well you don't do nothing in the created thread except creating an object which finally ends up in heap (which is shared between threads), but since the reference to it is to not maintained after run, then it's also lost.

    Your @Subscribe method from myObject is invoked in the same thread that calls eventBus.post(event); and not in the thread that created myObject.

    The weird thing I am finding is that all the actions/logging from my subscribed method inside of MyObject class are executing in the main thread again

    If your MyObject class has an @Subscribe method, then why does it need an instance of EventBus inside the constructor? You probably want

    MyObject myObject = new MyObject();
    eventBus.register(myObject);`
    

    instead of MyObject myObject = new MyObject(eventBus);