A thread that will keep running?

user3482468

I am writing some Android code, Here is the structure: (Pseudo)

public class AThread extends Thread {
  public void run() {
    int i = 0;
    while(true){
      i++;
      System.out.println(i);
    }  
  }
}

Then I new a instance of AThread class and call the start() method. My question is: The int i doesn't seem to keep adding in the while loop. Why is the reason and how can I make the thread keep running the task written in the while loop.

Because I am running some real-time analysis function...

Here is the actual one I wrote:

    int i = 0;
    running = true;
    while(running) {
        i++;
        System.out.println(i);            
        if((lines = bufferedReader.readLine()) != null) {
            System.out.println("analyzing");
            // read two lines per time.
            result = filter(lines, bufferedReader.readLine());
            if(!result[0].equals("emp")) {
                System.out.println("x: " + result[0] + " " + "y: " + result[1]);
                System.out.println(MainActivity.analysisKeyPress.classify(Integer.valueOf(result[0]), Integer.valueOf(result[1]), "0"));
                System.out.println("insert into db..");
                MainActivity.dbhelper.addKeyLog(getCurrentRunningService(), result[0], result[1]);
            }
        }
    }

It seems that the int i won't increase till there are several lines for BufferedReader to read.

nhaarman

If you comment out this part:

result = filter(lines, bufferedReader.readLine());
if(!result[0].equals("emp")) {
    System.out.println("x: " + result[0] + " " + "y: " + result[1]);
    System.out.println(MainActivity.analysisKeyPress.classify(Integer.valueOf(result[0]), Integer.valueOf(result[1]), "0"));
    System.out.println("insert into db..");
    MainActivity.dbhelper.addKeyLog(getCurrentRunningService(), result[0], result[1]);
}

the loop will work.

So obviously, some method in that part blocks your code. Try running a debugger and step-by-step execute the code to see where you code stops executing. That's why your loop doesn't continue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related