Skip to content

Instantly share code, notes, and snippets.

View amitshekhariitbhu's full-sized avatar
🎯
Helping developers in getting high-paying tech jobs

AMIT SHEKHAR amitshekhariitbhu

🎯
Helping developers in getting high-paying tech jobs
View GitHub Profile
public class SubscriptionActivity extends Activity {
Subscription subscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subscription = getObservable()
.subscribeOn(Schedulers.io())
/* Here first of all, we get the list of users from server.
* Then for each userId from user, it makes the network call to get the detail
* of that user.
* Finally, we get the userDetail for the corresponding user one by one
*/
/*
* This observable return the list of users.
*/
private Observable<List<User>> getUserListObservable() {
/*-------------- Getting the userList ----------------*/
RxAndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
.addPathParameter("pageNumber", "0")
.addQueryParameter("limit", "3")
.build()
.getParseObservable(new TypeToken<List<User>>() {}) // This returns you Observable
.subscribeOn(Schedulers.io()) // do the network call on another thread
.observeOn(AndroidSchedulers.mainThread()) // return the result in mainThread
.subscribe(new Observer<List<User>>() {
@Override
/*
* do some task at high priority
*/
public void doSomeTaskAtHighPriority(){
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.submit(new PriorityRunnable(Priority.HIGH) {
@Override
public void run() {
// do some background work here at high priority.
}
public class DefaultExecutorSupplier{
private final PriorityThreadPoolExecutor mForBackgroundTasks;
private DefaultExecutorSupplier() {
mForBackgroundTasks = new PriorityThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
public class PriorityRunnable implements Runnable {
private final Priority priority;
public PriorityRunnable(Priority priority) {
this.priority = priority;
}
@Override
public void run() {
/*
* Get the future of the task by submitting it to the pool
*/
Future future = DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.submit(new Runnable() {
@Override
public void run() {
// do some background work here.
}
});
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable runnable) {
handler.post(runnable);
}
}
public class PriorityThreadFactory implements ThreadFactory {
private final int mThreadPriority;
public PriorityThreadFactory(int threadPriority) {
mThreadPriority = threadPriority;
}
@Override
public Thread newThread(final Runnable runnable) {
/*
* Using it for Background Tasks
*/
public void doSomeBackgroundWork(){
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some background work here.
}