Skip to content

Instantly share code, notes, and snippets.

View vvsevolodovich's full-sized avatar

Vladimir Ivanov vvsevolodovich

View GitHub Profile
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 09:00
Background with Bus
public class Background {
private final Bus mEventBus;
public void postEvent(final Object event) {
mEventBus.post(event);
}
}
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 08:58
Background with Handler
public class Background {
...
private final Handler mUiHandler;
public void postOnUiThread(final Runnable runnable) {
mUiHandler.post(runnable);
}
}
@vvsevolodovich
vvsevolodovich / Background.java
Created March 12, 2018 08:57
Background class first attempt
public class Background {
private final ExecutorService mService = new ScheduledThreadPoolExecutor(5);
public Future<?> execute(Runnable runnable) {
return mService.submit(runnable);
}
public <T> Future<T> submit(Callable<T> runnable) {
return mService.submit(runnable);
}
private static final class PerThreadQueuedDispatcher extends Dispatcher {
private final ThreadLocal<Queue<Event>> queue =
new ThreadLocal<Queue<Event>>() {
@Override
protected Queue<Event> initialValue() {
return Queues.newArrayDeque();
}
};
public class EventBus {
private final SubscriberRegistry subscribers = new SubscriberRegistry(this);
public void register(Object object) {
subscribers.register(object);
}
public void unregister(Object object) {
subscribers.unregister(object);