title: Android volatile not working?
body: <p>I have an Activity class, in which I have a static flag, let's say</p>
  
  <pre><code>public static volatile flag = false;
  </code></pre>
  
  <p>Then in the class, I start a thread, which checks the flag and do different things.</p>
  
  <p>I also have a broadcastreceiver, which sets the flag to true or false. </p>
  
  <p>I though volatile will force the flag to the most recent value. But I can see my broadcastreceiver sets the static flag to true, but my thread is still getting it as false.</p>
  
  <p>Am I missing something basic here? Any help would be appreciated!</p>
  
  <p>Simplified Code (Updated) - So the flag is supposed to change to true after one minute. But it never did. But message from broadcast receiver shows it has been change to true</p>
  
  <p><strong>TestappActivity.java:</strong></p>
  
  <pre><code>package com.test;
  
  import java.util.Calendar;
  
  import android.app.Activity;
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Intent;
  import android.os.Bundle;
  
  public class TestappActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Intent intent0 = new Intent(this, TestService.class);
          this.startService(intent0);
  
          Intent intent = new Intent(this, TestReceiver.class);
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          PendingIntent sender = PendingIntent.getBroadcast(this,
                  1, intent,
                  PendingIntent.FLAG_UPDATE_CURRENT);
          Calendar slot = Calendar.getInstance();
          int min = slot.get(Calendar.MINUTE);
          slot.set(Calendar.MINUTE, min+1);
          am.set(AlarmManager.RTC_WAKEUP, slot.getTimeInMillis(), sender);
      }
  }
  </code></pre>
  
  <p><strong>TestService.java:</strong></p>
  
  <pre><code>package com.test;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  
  public class TestService extends Service {
  
      private static final String TAG = "TestService";
  
      public static volatile boolean flag = false;
  
      private MyTopThread mTopThread;
  
      public TestService() {
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
  
      @Override
      public void onCreate() {
  
      }
  
      @Override
      public void onDestroy() {
  
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          protect();
  
          // We want this service to continue running until it is explicitly
          // stopped, so return sticky.
          return START_STICKY;
      }
  
  
      /**
       * Run protection
       * 
       */
      private void protect() {
  
          mTopThread = new MyTopThread();
          mTopThread.start();
      }
  
  
      private class MyTopThread extends Thread {
  
          @Override
          public void run() {
              while (true) {
                  try {
                      Thread.sleep(150);
                      Log.d(TAG, "Flag is " + TestService.flag);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
  
              }
          }
  
      }
  }
  </code></pre>
  
  <p><strong>TestReceiver.java:</strong></p>
  
  <pre><code>package com.test;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.util.Log;
  
  public class TestReceiver extends BroadcastReceiver {
      final static private String TAG = "TestReceiver";
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.d(TAG, "onReceive is triggered ...");
          TestService.flag = true;
          Log.d(TAG, "flag is changed to " + TestService.flag);
  
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="8" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name=".TestappActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;service android:name=".TestService" /&gt;
  
          &lt;receiver
              android:name=".TestReceiver"
              android:process=":remote" &gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>