Add threads in awaited for loops. Remove new threads from Requestor.getObject calls

This commit is contained in:
Clayton Wilson 2020-10-25 16:53:51 -04:00
parent 6b30476ccd
commit c07599ecc6
3 changed files with 64 additions and 29 deletions

View File

@ -88,22 +88,50 @@ public class ItemDetails extends AppCompatActivity implements ListPickerDialogFr
Requestor requestor = new Requestor(am, configs.getProperty("apiKey")); Requestor requestor = new Requestor(am, configs.getProperty("apiKey"));
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>(); SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver); requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver);
Thread t = new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Integer[] listIds = null;
try { try {
Integer[] listIds = listIdsReceiver.await(); listIds = listIdsReceiver.await();
for (int i = 0; i < listIds.length; i++) {
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
shoppingLists.add(listReceiver.await());
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// Create threads and add them to a list
Thread[] threads = new Thread[listIds.length];
List[] results = new List[listIds.length];
for (int i = 0; i < listIds.length; i++) {
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
int finalI = i;
Thread l = new Thread(new Runnable() {
@Override
public void run() {
try {
results[finalI] = listReceiver.await();
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads[i] = l;
l.start();
}
// Wait for each thread to finish and add results to shoppingLists
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
shoppingLists.add(results[i]);
}
loadingDialog.cancel(); loadingDialog.cancel();
ListPickerDialogFragment listPickerDialog = new ListPickerDialogFragment(shoppingLists); ListPickerDialogFragment listPickerDialog = new ListPickerDialogFragment(shoppingLists);
listPickerDialog.show(getSupportFragmentManager(), "User Lists"); listPickerDialog.show(getSupportFragmentManager(), "User Lists");

View File

@ -108,14 +108,7 @@ public class SearchResults extends AppCompatActivity implements SortDialogFragme
// Clear old search results from the view // Clear old search results from the view
resultsProductListSorted.clear(); resultsProductListSorted.clear();
searchResultsListAdapter.notifyDataSetChanged(); searchResultsListAdapter.notifyDataSetChanged();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
doSearch(query); doSearch(query);
}
});
t.start();
return false; return false;
} }

View File

@ -66,13 +66,7 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>(); SynchronousReceiver<Integer[]> listIdsReceiver = new SynchronousReceiver<>();
final Requestor.Receiver<Integer[]> recv = this; final Requestor.Receiver<Integer[]> recv = this;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
requestor.getListOfIds(List.class, recv, null); requestor.getListOfIds(List.class, recv, null);
}
});
t.start();
FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab); FloatingActionButton fab = (FloatingActionButton) root.findViewById(R.id.new_list_fab);
@ -144,17 +138,37 @@ public class ListsFragment extends Fragment implements CreateListDialogFragment.
@Override @Override
public void acceptDelivery(Object delivered) { public void acceptDelivery(Object delivered) {
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
Integer[] listIds = (Integer[]) delivered; Integer[] listIds = (Integer[]) delivered;
try { // Create threads and add them to a list
// Integer[] listIds = listIdsReceiver.await(); Thread[] threads = new Thread[listIds.length];
List[] results = new List[listIds.length];
for (int i = 0; i < listIds.length; i++) { for (int i = 0; i < listIds.length; i++) {
SynchronousReceiver<List> listReceiver = new SynchronousReceiver<>();
requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver); requestor.getObject(Integer.toString(listIds[i]), List.class, listReceiver, listReceiver);
shoppingLists.add(listReceiver.await()); int finalI = i;
} Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
results[finalI] = listReceiver.await();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}
});
threads[i] = t;
t.start();
}
// Wait for each thread to finish and add results to shoppingLists
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
shoppingLists.add(results[i]);
}
// Set adapter and display this users lists // Set adapter and display this users lists
displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists); displayShoppingListsAdapter = new DisplayShoppingListsAdapter(getActivity(), shoppingLists);