Android streams

Author: Eugeniy Marilev
Date of publication: 2014-05-06 10:38:16
Tools of the Java language for streaming is quite powerful and logical. If you are developing applications for Android in Java, then you will not need to learn anything new about the implementation of streams. The following example demonstrates the standard version of the streaming for Android:
class MyRunnable implements Runnable {
     public void run() {
         //Place some code inside thread
//Launching the code inside the current stream
Runnable process = new MyRunnable();;
//Launching in a parallel stream
Thread process = new Thread(new MyRunnable(), «MyRunnable#process»);
There's only one problem, which, certainly, a large number of Java developers are facing. The point is — you can not work simultaneously with the elements of the GUI on multiple streams. That means, for example, if you want to make a list (ListView), elements of which must be completed in two or more parallel threads, you will not be able to make this task as easy as it is done in Java SE. in direct address to the interface element of the party thread, the main thread, which runs GUI will fall asleep. This will cause the user to observe the application hung until the third-party streams adressing to GUI will go to sleep or run out. Luckily, in Android the possibility of deferred calling code outside the stream is provided. The principle of deferred call is pretty simple:
  1. The wrong stream creates a message object.
  2. The message object is placed in the message queue of the main stream.
  3. The main stream when it is free, would get a message from the queue and fulfill the necessary instructions.
Here's a simple approach. And the implementation is also quite simple:
package com.mycompany.androidtest;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;
 * AndroidTestProjectActivity is the activity class for providing
 * main application screen functionality
public class AndroidTestProjectActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    protected function init() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                Handler handler = new Handler() {
                    public void handleMessage(Message msg) {
                        //place some code for GUI processing here
                Message msg = new Message();
                msg.obj = this;
               //handler.sendMessageDelayed(msg, delayMillis);
That means that it's possible to send a scheduled message, with indicating the approximate time - «sendMessageDelayed», when to perform a task or if you want you can schedule it for immediate fulfillment - «sendMessage» (but in fact it will not be immediate fulfillment). Also see other methods of Handler's class (sendMessageAtFrontOfQueue, sendMessageAtTime). In addition to the described method, there is also native solution for Android - a class AsyncTask, the use of which leads to certain advantages, and, of course, disadvantages. If you are interested, we can take a look at the specific example to find out how to implement the filling listView in parallel streams. Such a restriction on the graphics processing exists only in the main stream and in all *nix-systems, that's why probably Android inherited this behavior. This happens because application communicates with the graphics with help of x-Server. X-server receives only a single simultaneous treatment of the application's context, otherwise application starts to freeze. Summing up all that is said I would like to give you a piece of good advice to develop in terms of developing multi-stream applications, because it improves the performance of applications, provides additional space for thinking, and in general for the future of multi-stream applications. Let's do it!
Article comments
No results found.
Only logged users can leave comments.