Use Executor thread pool instead of creating my own threads on the fly

This commit is contained in:
Anthony Restaino 2016-01-30 22:46:57 -05:00
parent 135cf2e572
commit 4a21d3f4f9
8 changed files with 57 additions and 49 deletions

View File

@ -328,14 +328,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearch.setOnEditorActionListener(search);
mSearch.setOnTouchListener(search);
new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
initializeSearchSuggestions(mSearch);
}
}).run();
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START);
@ -1480,6 +1480,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* previously searched URLs
*/
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {
mSearchAdapter = new SearchAdapter(this, mDarkTheme, isIncognito());
getUrl.post(new Runnable() {
@Override
public void run() {
getUrl.setThreshold(1);
getUrl.setDropDownWidth(-1);
getUrl.setDropDownAnchor(R.id.toolbar_layout);
@ -1514,9 +1520,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
});
getUrl.setSelectAllOnFocus(true);
mSearchAdapter = new SearchAdapter(this, mDarkTheme, isIncognito());
getUrl.setAdapter(mSearchAdapter);
}
});
}
/**
* function that opens the HTML history page in the browser

View File

@ -16,6 +16,7 @@ public class BrowserApp extends Application {
private static AppComponent mAppComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor();
private static final Executor mTaskThread = Executors.newCachedThreadPool();
@Inject Bus mBus;
@ -41,6 +42,11 @@ public class BrowserApp extends Application {
return mIOThread;
}
@NonNull
public static Executor getTaskThread() {
return mTaskThread;
}
public static Bus getBus(@NonNull Context context) {
return get(context).mBus;
}

View File

@ -228,7 +228,7 @@ public class DownloadHandler {
} else {
final DownloadManager manager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
new Thread() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
try {
@ -243,7 +243,7 @@ public class DownloadHandler {
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download));
}
}
}.start();
});
eventBus.post(new BrowserEvents.ShowSnackBarMessage(
context.getString(R.string.download_pending) + ' ' + filename));
}

View File

@ -137,7 +137,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
exportpref.setOnPreferenceClickListener(this);
importpref.setOnPreferenceClickListener(this);
new Thread(mInitializeImportPreference).start();
BrowserApp.getTaskThread().execute(mInitializeImportPreference);
}
private final Runnable mInitializeImportPreference = new Runnable() {

View File

@ -170,7 +170,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
// Must be called here, only here we have a reference to the ListView
new Thread(mInitBookmarkManager).run();
BrowserApp.getTaskThread().execute(mInitBookmarkManager);
return view;
}

View File

@ -175,13 +175,12 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Thread clear = new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
clearCookies();
}
});
clear.start();
}
})
.setNegativeButton(getResources().getString(R.string.action_no), null).show();

View File

@ -96,12 +96,10 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
mSearchSubtitle = mContext.getString(R.string.suggestion);
mDarkTheme = dark || incognito;
mIncognito = incognito;
Thread delete = new Thread(new ClearCacheRunnable(BrowserApp.get(context)));
BrowserApp.getTaskThread().execute(new ClearCacheRunnable(BrowserApp.get(context)));
mSearchDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_search, mDarkTheme);
mBookmarkDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_bookmark, mDarkTheme);
mHistoryDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_history, mDarkTheme);
delete.setPriority(Thread.MIN_PRIORITY);
delete.start();
mLanguage = Locale.getDefault().getLanguage();
if (mLanguage.isEmpty()) {
mLanguage = DEFAULT_LANGUAGE;

View File

@ -55,7 +55,7 @@ public class AdBlock {
}
private void loadBlockedDomainsList(@NonNull final Context context) {
Thread thread = new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
@ -77,7 +77,6 @@ public class AdBlock {
}
}
});
thread.start();
}
/**
@ -140,7 +139,7 @@ public class AdBlock {
* @param context the context needed to read the file
*/
private void loadHostsFile(@NonNull final Context context) {
Thread thread = new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
@ -181,6 +180,5 @@ public class AdBlock {
}
}
});
thread.start();
}
}