Hello to all. I am writing a small interpreter in which there are several functions, including the sleep () function, as well as a TextView, which serves as the log role. For example, there is a code for the interpreter:

printline("test") // пишет в лог текст sleep(1000) // "спит" секунду 

The execution looks like this: The lineWorker method starts, which in turn twists the code. The problem is that EditText will be updated only when the program ends and the lineWorker method stops (in this case, the word test will be displayed only after a second). But it is necessary that the interface be updated with each function worked out in lines of code. How to get out of this situation?

  • I think I understand what the problem is, but I'm not sure .. How do you sleep going? Thread.sleep(long millis) ? - Flippy
  • Exactly ... the essence is different here, any capacious task will make the whole flow wait. Here it is not evil, but the fact that all the code of the interpreter is executed in one method. I need to create a second thread that will do this. Which I try - matMoteX
  • Tried a bunch of Thread/Handler ? - Flippy

1 answer 1

You need to create a new thread and, after a pause, output data via the post method. The most primitive example would look like this:

 new Thread(new Runnable(){ public void run() { for (int i=0;i<n;i++){ doWork(); Thread.sleep(1000); textView.post(new Runnable(){ public void run() { printline("test"); } }); } } }); 

And I don’t quite understand why you are using EditText for the log and not TextView .