I found a very peculiar problem when creating a thread launch inside an Android application.
If I have the following class:
public class TroubleThread extends Thread{ boolean running; public boolean isRunning() { return running; } public void setRunning(boolean running) { this.running = running; } @Override public void run() { while (isRunning()){ }
and add it somewhere in the Activity onCreate (...) method, for example:
public class MyActivity extends Activity { TroubleThread myThread; @Override public void onCreate(Bundle savedInstanceState) {
application crashes.
But if I changed the run () method to:
@Override public void run() { while (running){
he stops from crashing.
Even if I solved my problem using locks, notify () and wait (), the question remains:
Why does the application continue to work when using direct access to the field, but does it fail when using the method?
source share