public
class
RemoteService {
private
static
int
cores
= Runtime.getRuntime().availableProcessors();
private
static
ExecutorService executor
= Executors.newFixedThreadPool(cores +
1
);
public
void
stop() { executor.shutdown(); }
public
void
getUserRecentGfgActs(ResultCallback callback)
{
executor.execute(() -> {
List<Like> gfgLikes =
new
ArrayList<>();
List<Post> gfgPOsts =
new
ArrayList<>();
List<Comment> comments =
new
ArrayList<>();
List<Friend> gfgCourses =
new
ArrayList<>();
Future<List<Like> > futureGfgLikes
= executor.submit(getGfgLikes(
Future<List<Comment> > futureComments
= executor.submit(getComments(
Future<List<Post> > futureGfgPOsts
= executor.submit(getGfgPOsts(
Future<List<Friend> > futureGfgCourses
= executor.submit(getGfgCourses(
try
{
gfgLikes = futureGfgLikes.get();
}
catch
(InterruptedException
| ExecutionException e) {
e.printStackTrace();
}
try
{
gfgPOsts = futureGfgPOsts.get();
}
catch
(InterruptedException
| ExecutionException e) {
e.printStackTrace();
}
try
{
comments = futureComments.get();
}
catch
(InterruptedException
| ExecutionException e) {
e.printStackTrace();
}
try
{
gfgCourses = futureGfgCourses.get();
}
catch
(InterruptedException
| ExecutionException e) {
e.printStackTrace();
}
List<Activity> gfgActs =
new
ArrayList<>();
gfgActs.addAll(gfgLikes);
gfgActs.addAll(gfgPOsts);
gfgActs.addAll(comments);
gfgActs.addAll(gfgCourses);
Collections.sort(
gfgActs,
(activity1, activity2)
-> activity1.getCreatedAt().compareTo(
activity2.getCreatedAt()));
callback.onResult(gfgActs);
});
}
private
Callable<List<Like> > getGfgLikes(String url)
{
return
() ->
{
System.out.println(
"getGfgLikes"
);
Thread.sleep(
200
);
return
Arrays.asList(
new
Like(
new
Date(1534334348560L)),
new
Like(
new
Date(1554365436546960L)));
};
}
private
Callable<List<Post> > getGfgPOsts(String url)
{
return
() ->
{
System.out.println(
"getGfgPOsts"
);
Thread.sleep(
500
);
return
Arrays.asList(
new
Post(
new
Date(15334343448560L)),
new
Post(
new
Date(153343434460L)));
};
}
private
Callable<List<Comment> > getComments(String url)
{
return
() ->
{
System.out.println(
"getComments"
);
Thread.sleep(
200
);
return
Arrays.asList(
new
Comment(
new
Date(15356565648560L)),
new
Comment(
new
Date(151454545456460L)));
};
}
private
Callable<List<Friend> >
getGfgCourses(String url)
{
return
() ->
{
System.out.println(
"getGfgCourses"
);
Thread.sleep(
6500
);
return
Arrays.asList(
new
Friend(
new
Date(1534543354248560L)),
new
Friend(
new
Date(15334343546460L)));
};
}
}