android


RunOnUiThread inside loop efficiency


Is it fine to do following if we think about efficiency?
new Runnable() {
#Override
public void run() {
try {
final Runnable uitask = new Runnable() {
public void run() {
//sth that modifies UI
}
};
for (...) {
context.runOnUiThread(uitask);
Thread.sleep(...);
}
} catch (Throwable t) {
}
}
}
I'm most concerned about that runOnUi might create new thread each time, which would be very bad? Am I right or it smarter like ThreadPool?
Well, runOnUiThread doesn't create new threads, it is starting your runnable on the existing UI thread.
but instead of your code you could just do
void onCreate(..) {
...
mHandler = new Handler();
}
for (...) {
mHandler.postDelayed(uitask, DELAY * i++);
}
I'm most concerned about that runOnUi might create new thread each
time, which would be very bad?
Well. It depends what you want to achieve. In Android, native threads are not much efficient. There are more effective ways how to do background work (SDK provides them).
If you want to do some periodical work you can use Handler to achieve your goal. Just create only one Runnable and then you can send request for periodical callbacks. Another solution is to use Services which run in background. They're directly designated for long-running tasks and provide very handy way for background processing.
Sometimes you need to inform user with some information about current progress in your tasks so in this case you can simply use mentioned Handler or AsyncTask.
I think your solution is not clean and efficient. If you'll create for example 40 Threads, your work won't be faster or more efficient at all. Your GB will scream and application will take the greater part of CPU and high battery consumption is something you shouldn't allow.

Related Links

How to disable Copy and Share Menu in Android
Android layouts - Would it be reasonable to create more Layout Folders than sw320, sw480 sw600 sw720 sizes?
Android Paint Object Memory Efficency
Scrollbars no longer visible in scrollable elements in Android System Webview 55.0.2883.91 for Android 5.1.1
4x4 GridLayout populate programatically in Android
using serial number security in android app using delphi
How to implement this(BufferedWriter) using Volley?
Android google map overlay on top
Android WebView cache mechanism
Android Studio No resource found #drawable
Glide Image List not working in Android
Android Application Proguard Rules
How to play and pause video in a videoview when the listitem of listview is completely visible on screen
Adb install fails, but takes space anyways
Can we post our own ads in Android apps? Is that possible
Push notification not receiving in Cordova Android App

Categories

HOME
go
fabricjs
phpword
kentico
delphi-7
cheerio
string
sublimetext
vscode-extensions
seo
carousel
onclick
azure-container-service
pcl-crypto
jsonpath
keytool
event-sourcing
entitymanager
symfony-2.8
wolframalpha
jmp
cfml
poedit
springfox
squashfs
dataflow-diagram
ios10.2
ckfinder
definitelytyped
licensing
progress-bar
paperjs
mql
recurring-events
cart
spring-security4
papaparse
hreflang
biztalk-rule-engine
browser-extension
apiary.io
nohup
projects-and-solutions
setup.py
plaintext
wmp
microsoft-metro
outputcache
xmldocument
httr
tinymce-3
filehandle
type-theory
mach-o
django-1.10
pocketsphinx-android
vcloud-director-rest-api
chromium-os
builtins
latin
neon
eigenvector
freepbx
aspen
np-spring
spring-retry
tfs-workitem
themoviedb-api
computer-architecture
zen-cart
slash
jdk1.7
aspectj-maven-plugin
osc
android-broadcast
java-security
openbabel
cryptographichashfunction
exceldatareader
mass-assignment
ejb-2.x
showdialog
jbake
big-ip
easyrtc
psr-4
imagefilter
build-script
grails-plugin-rabbitmq
paste
vbe
dsym
koala
conditionaltagsupport
sbcl
android-audiorecord
eclipse-kepler
oocss
alice
dopostback
frameworkelementfactory
jquery-blockui
xcode3.2
node-redis
flex-mobile
umra
uipagecontrol
updatesourcetrigger
office-2007
web-garden
web-application-project
escrow
wysiwym
ifilter

Resources

Encrypt Message