Skip to main content
clarify
Source Link
David Rawson
  • 21.2k
  • 9
  • 93
  • 130

When you call wait() inside a class, you are actually calling wait() on the current instance of that class. To do that, it requires a lock on that instance. If another thread has the lock, you will not be able to wait.

Adding synchronized to onCreate() will acquire the lock (the current instance of the Activity) and let you call wait() inside, but this is definitely not want you want to do as it will block the main/UI thread and cause your app to be non-responsive.

What you probably want instead is the following inside your activity:

private boolean isTerminationConditionMet = false;

@Override
public void onCreate() {
    //standard Android onCreate code 

    final Handler handler = new Handler();
handler.postDelayed(    final Runnable task = new Runnable() {
        @Override
        public void run() {
            //code you want to run afterevery thesecond
 delay           if (!isTerminationConditionMet()) {
                handler.postDelayed(task, 1000);
            }
        };
    }
    handler.postDelayed(task, 1000);
}

This will cause the code inside run() to be run after the desired delay (1000ms) and then every 1000 ms until your termination condition is met.

Adding synchronized to onCreate() will acquire the lock (the current instance of the Activity) and let you call wait() inside, but this is definitely not want you want to do as it will block the main/UI thread and cause your app to be non-responsive.

What you probably want instead is the following:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //code you want to run after the delay
    }
};
, 1000);

This will cause the code inside run() to be run after the desired delay (1000ms)

When you call wait() inside a class, you are actually calling wait() on the current instance of that class. To do that, it requires a lock on that instance. If another thread has the lock, you will not be able to wait.

Adding synchronized to onCreate() will acquire the lock (the current instance of the Activity) and let you call wait() inside, but this is definitely not want you want to do as it will block the main/UI thread and cause your app to be non-responsive.

What you probably want instead is the following inside your activity:

private boolean isTerminationConditionMet = false;

@Override
public void onCreate() {
    //standard Android onCreate code 

    final Handler handler = new Handler();
    final Runnable task = new Runnable() {
        @Override
        public void run() {
            //code you want to run every second
            if (!isTerminationConditionMet()) {
                handler.postDelayed(task, 1000);
            }
        }
    }
    handler.postDelayed(task, 1000);
}

This will cause the code inside run() to be run after the desired delay (1000ms) and then every 1000 ms until your termination condition is met.

Source Link
David Rawson
  • 21.2k
  • 9
  • 93
  • 130

Adding synchronized to onCreate() will acquire the lock (the current instance of the Activity) and let you call wait() inside, but this is definitely not want you want to do as it will block the main/UI thread and cause your app to be non-responsive.

What you probably want instead is the following:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //code you want to run after the delay
    }
};
, 1000);

This will cause the code inside run() to be run after the desired delay (1000ms)