How to implement Multithreading in Android application so as to increase responsiveness of UI. ?
UI in Android is already handled in a separate thread, so you don't need to worry about it. The faq has a section on Handling Expensive Operations in the UI Thread if that is what you need to solve.
I suppose you want to do some long and consuming task in the background so as not to lock the UI thread.
AsyncTask seems easier to use even though there are other approaches like using runnables and handlers.
Actors or similar event-based, managed lightweight threads are nice to treat UI events in a low-overhead fashion.
Related
It might be a silly question but I need to ask it to clarify my ideas.
I've studied during my briefly study on Android Development how to set a background Thread.
Now, I've used both Async Task and Handler + Runnable/Message. Which of those two is the most used or better the most recommended?
Thank you so much.
Both the utilities have different functionalities while Async Task is used for background processing for short operations , Handler operates on the main thread for communicating from the background thread.
AFAIK, android uses Single thread architecture, where all UI components event are dispatched,i.e. main thread or UI thread.
But why we cannot perform UI operations on separate thread other than main thread?
The main problem is related to the context. In a background thread you could update some UI stuff which is not in the current context. This is one of the reason.
UPDATE:
From the Android developer guide:
Additionally, the Andoid UI toolkit is not thread-safe. So, you must
not manipulate your UI from a worker thread—you must do all
manipulation to your user interface from the UI thread. Thus, there
are simply two rules to Android's single thread model:
Do not block the UI thread
Do not access the Android UI toolkit from outside the UI thread
You can read more on thread safety from [1] [2] and you can read even [3] with a small exmaple and explenation!
Hope that now it is more clear sorry for the short and quick answer before :)
Well, first of all I need to state that this is a design level problem that has been discussed by many generations of programmers :)
Now technical details:
A GUI framework is a very complex thing. A GUI framework needs to display (draw) certain views (or windows) and route external (mostly user) inputs to the view that the input is targeted to. Now, to manage the screen with views with proper event dispatch to each view, all views shall be part of a graph and the simplest form of such a graph is a tree. So, you essentially have a tree of views to manage - where information should be fed to a root view from where your whole event routing takes place to child views.
Naturally, one dedicated thread to do manage this view hierarchy and event routing by monitoring a message queue to which all these drawing and input events are posted - by itself or any other threads - is a neat and simple solution. Any other solution that involves more than a thread will make the already complex view management and event routing code prone to dead/live locks.
Along with these technical details, you need to keep it in mind that you are trying to do business with a framework which is going to be used by other people who usually don't have faintest idea about the inner details of your architecture. Introduce threading support in such a framework is an open invitation for synchronization bugs (read: deadlocks, grumpy end users who are programmers AND ultimately bad business).
Android (and 99% other GUI toolkits) adopts this method. Simply because it makes things simpler and less error prone to have one main thread to deal with all error prone processing. All other threads are free to request it to do things by posting messages to the main thread's message queue. It is a balance between complexity and stability.
The only drawback of this approach is smooth updates of views - if you have many views to update simultaneously, OR your view hierarchy itself is really nested and complex. This drawback of this single threaded architecture manifest itself in pretty obvious forms in Android like:
Animating multiple views simultaneously is never smooth - unlike iOS where they have taken the pains to move certain parts of this operation to more than one threads of execution.
Android documentation need to remind the programmer all the time not to do lengthy operations in main thread AND to avoid deeply nested view hierarchies.
As we know, when we update the UI from the non-ui-thread, we use Handler or AsyncTask. We can find a lot of articles on how to use these methods on the Internet. But I cannot find an explanation on why a UI element cannot be operated from the non-ui thread? Can anyone help me?
I believe this decision was made by the Android team (and many other UI frameworks for that matter) because of the following reasons
Synchronization
Security
Performance
Synchronization
The simplest reason for following a single threaded model for UI is that it is the easiest way to ensure that the User Interface is not being updated by multiple sources at once and therefore, corrupted. If you imagine that multiple threads can modify the UI, it would take each thread its own amount of time to execute a portion of its code, and with the different execution speeds generate a bad user experience.
Security
Ensuring that one thread can access the UI is also a security measure, preventing any slave threads that accidentally (or purposefully) try to corrupt the UI from doing so, simply by not allowing it.
Performance
The core fact of the matter here is that UI operations, and re-rendering and re-drawing visual layers and elements is an expensive process. It can affect the performance of the framework and cause leaks or lags with the deadlocks and synchronisation in-between. So I believe this was done for the sake of performance too :)
I suspect it's because a UI is a state machine, having multiple threads operate on a single state machine makes it difficult or impossible to reason about the current state of the UI and what transitions are available at any given time. There would be unpredictable results, so it's best to keep it separated from code executing in an AsyncTask.
writing a small android game for a university assignment and just wanted to check what I'm doing is correct.
I'm creating a Mastermind type game and am unsure how to handle basic game operations such as checking if a guess is correct.
Am I correct in thinking that the best way to handle these operations is to create inner classes that extend Async to avoid UI lockup?
If not could anyone suggest any other ways?
Cheers!
After many weeks of developing my application, AsyncTask is what I used instead of standard threads. AyncTasks are less likely to get killed, they create a worker thread and handlers by themselves so you can update GUI through it more easily than standard threads. But remember that only one instance of AsyncTask can work at a time.
I don't think you need AsyncTask for this game at all. AsyncTask is a process for which you do not know how long it will take and you don't want to block your UI thread, like downloading something from Internet.
Your game can be completly events driven. Whenever the user makes a move you call your methods.
If you want to have animations while the user thinks then SurfaceView can do the job.
Short verson
Is it allowed, or do I need to use the UI thread?
EDIT: A reference to a place in the official android docs would be ideal.
Long version
Android docs make it clear that it's not allowed to "access the Android UI toolkit from outside the UI thread".
On the other hand, creating Bitmap objects from worker threads seems to be allowed, at least it's being done in sample code:
http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
Apparently, the Bitmap class is not considered part of the "UI toolkit" as far as threading is concerned.
I've got a piece of code that seems to work when invoked from a non-ui thread. It involves using Bitmap.createBitmap(int,int,Bitmap.Config), new Canvas(bitmap), Typeface.create() and text drawing. My code does not refer to any View object.
Can someone point me to a piece of documentation that says that I may do these things from a background thread? Or will doing this lead to random crashes?
UI toolkit means UIs such as buttons, labels, listview, and so on provided by Google. You can't access them from non-ui thread mainly because they are not thread-safe.
What you are doing is not on UI toolkit but on low-level Canvas which is allowed (actually should be allowed) to access from non-ui threads. This mechanism is used in game development all the time. So I believe you are safe.
Updating views has to be made on UI thread, or from remote thread with post function (which basiclly tells UI thread that remote thread wants something to be done), which is part of View class.
It should be legal as bitmaps just sit in memory all by themselves before you draw them.
I am looking into game development, in it seems there are some threads updating the UI ther too (using a similar technique of doing in-memory rendering)