hehonghui / AndroidEventBus

A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.
Apache License 2.0
1.61k stars 391 forks source link

ThreadMode.ASYNC executes in one thread only #11

Open vixtor opened 9 years ago

vixtor commented 9 years ago

Thanks for the great library! However something is bothering me. I am posting a message to three different methods, each has its own tag and they are set to ThreadMode.ASYNC. However they do not execute in parallel, but sequentially.

EventBus.getDefault().post(7, "test1");
EventBus.getDefault().post(7, "test2");
EventBus.getDefault().post(7, "test3");

@Subscriber(tag = "test1", mode = ThreadMode.ASYNC)
public void test1(int request){
    ...
}
...

Logcat: 04-21 23:15:19.835 E/test1﹕ Working on a task 04-21 23:15:20.936 E/test1﹕ Done -2134183543 04-21 23:15:20.936 E/test2﹕ Working on a task 04-21 23:15:22.247 E/test2﹕ Done -2134183543 04-21 23:15:22.247 E/test3﹕ Working on a task 04-21 23:15:23.058 E/test3﹕ Done -2134183543

Expected something like this: 04-21 1.1 E/test1﹕ Working on a task 04-21 1.2 E/test2﹕ Working on a task 04-21 1.3 E/test3﹕ Working on a task 04-21 2.1 E/test1﹕ Done -2134183543 04-21 2.3 E/test2﹕ Done -2134183543 04-21 2.7 E/test3﹕ Done -2134183543

hehonghui commented 9 years ago

that's right, @vixtor 。all the message execute in a single thread when the thread mode is async.you can custom the async event handler like this :

public class ThreadPoolHandler implements EventHandler {
ExecutorService mExecutorService = Executors.newFixedThreadPool(3);
EventHandler mHandler = new DefaultEventHandler();
@Override
public void handleEvent(final Subscription subscription, final Object event) {
mExecutorService.submit(new Runnable() {
@Override
public void run() {
mHandler.handleEvent(subscription, event);
}
});
}
}

and then set async event handler with EventBus.getDefault().setAsyncEventHandeler(new ThreadPoolHandler());

vixtor commented 9 years ago

Thank you, this worked perfectly. If I may suggest, you could add this in the next version as ThreadMode.THREADPOOL or something similar.

hehonghui commented 9 years ago

That's great advice. We will consider putting it into our next version. I am collecting apps which adopt AndroidEventBus and they will be put in readme.md. Would it be too much to ask you to share with us your App name and download link? Thank you!

2015-04-22 18:31 GMT+08:00 Viktor Kvaternjak notifications@github.com:

Thank you, this worked perfectly. If I may suggest, you could add this in the next version as ThreadMode.THREADPOOL or something similar.

— Reply to this email directly or view it on GitHub https://github.com/bboyfeiyu/AndroidEventBus/issues/11#issuecomment-95124473 .

vixtor commented 9 years ago

Sure, I will send it once it is finished and published.

hehonghui commented 9 years ago

Thank you very much![?]

2015-04-23 4:23 GMT+08:00 Viktor Kvaternjak notifications@github.com:

Sure, I will send it once it is finished and published.

— Reply to this email directly or view it on GitHub https://github.com/bboyfeiyu/AndroidEventBus/issues/11#issuecomment-95324958 .

hehonghui commented 9 years ago

@vixtor hi,I am collecting apps which adopt AndroidEventBus and they will be put in readme.md. Would it be too much to ask you to share with us your App name and download link? Thank you!