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.
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();
}
}