Schwertspize
Schwertspize

Reputation: 151

Android: runOnUiThread mixing up output

I use a Process to run a native binary. That is not problem, and i can get the InputStream as normal and parse it via a Scanner. If i do while (scanner.hasNextLine()) and inside System.out.println(scanner.nextLine()), everything is fine. If I run System.out.println(scanner.nextLine()) inside runOnUiThread(), then it mixes up my output, i dont get everything, but what i get 20 times the same line (not the same number every run).

Here, my complete code:

processScanner = new Scanner(process.getInputStream());
while(processScanner.hasNextLine()) {
  line = processScanner.nextLine();
  System.out.println(line);
  runOnUiThread(new Runnable() {
      @Override
      public void run() {
          cuberiteSTDOUT.writeString(line);
      }
  });
}

creates normal output.


processScanner = new Scanner(process.getInputStream());
while(processScanner.hasNextLine()) {
  line = processScanner.nextLine();
  runOnUiThread(new Runnable() {
      @Override
      public void run() {
          System.out.println(line);
          cuberiteSTDOUT.writeString(line);
      }
  });
}

creates crazy output like

02-28 12:44:18.433 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.435 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.441 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.442 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.443 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.447 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.449 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.450 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.450 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.451 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.452 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.453 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.454 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.456 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}

(which is just some lines repeated but others left out)

Thanks in Advance

Upvotes: 0

Views: 41

Answers (1)

Henning Luther
Henning Luther

Reputation: 2075

Youre line is manipulated in another thread than it is printed. Change the line to:

final String line = processScanner.nextLine();

Upvotes: 2

Related Questions