• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Working With Multi-thread and Array

louislolo

Lurker
I'm having trouble with multi-threading here. I have a thread that run multiple times, so I know this is multi-threading. The program reads data from a file and stores them into an array. The method postData() post a set of array data to my website, and then it deletes the array so that the program can read another set of new data. The Send.send_data(); method send data to my website, and that works, so I don't think there is a problem there.
It seems like I'm not receiving the data in order on my website and I'm also getting repeated data being submitted, so I believe some data are overrided... I used the sleep() so, am I using multi-threads correctly here? But What should I change?
For example, when I submit set1, set2, set3. On my website, I'm receiving set3, set2, set3.




Code:
                    try 
                    {
                        FileInputStream fis = new FileInputStream(myInternalFile);
                        DataInputStream in = new DataInputStream(fis);
                        BufferedReader br = new BufferedReader(new InputStreamReader(in));
                        String strLine;

                        int i=0;
                        int internal_entries=0;
                        
                        while ((strLine = br.readLine()) != null ) 
                        {
                            Data.array_data[i]=strLine;

                           if(i==19)  //submit after collecting 0-19 results
                            {    
                                Thread t = new Thread(new Runnable()
                                {
                                    @Override
                                    public void run() 
                                    {
                                        postData();
                                    }
                                });
                                t.start();

                                i=-1;
                            }
                            i++;
                          }
                       in.close();
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
Code:
public void postData() 
    {
Send.send_data();
        try 
        {
            Thread.sleep(5000);

            for(int i=0; i<=19; i++)
            {
                Data.array_data[i]=null;
            }
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
It looks like you are creating a new thread every 20 lines of data read.

That thread begins to execute, but your while loop is still running on the original thread.

postData(); looks like it is trying to read data from an object on the original thread but running on a new thread, then it is waiting 5 seconds, then deleting this data (and the data exists on the original thread).

All this time, the while loop is still running and still reading data, and spawning more and more NEW threads.

-So you have something writing data to your array on "Thread 1"
-Something reading and sleeping and deleting data on "Thread 1" from "Thread 2"
-"Thread 1" is now spawning "Thread 3", (possibly before Thread 2 has completed, possibly after -- there is no way to know)


So the side effect is that you have created a volatile data set, it is being read from, and written to, at the same time by different threads (a type of race condition).

You have a few options:

1) HIGHLY RECOMMENDED Use a library to handle uploading and concurrency for you (best solution). Concurrency is very hard and there is no need to reinvent what many smart people have already collaboratively solved many times before.

Good libraries: Google Volley, Square Retrofit

This is what most experienced devs do.


2) Use an IntentService. This will spawn a single background worker thread for you and run until it is completed, then close the thread.

This is the old "Android" way



3) Wrap all your code to read data and upload data in your thread's run() method, not just the postData() method. This way you are not trying to manipulate data across thread boundaries.

This is the very old, manual Java way


Hope that helps.
 
Back
Top Bottom