複数のIntentServiceを使って非同期処理を同期化する

IntentServiceの特徴はTechBoosterさんの「IntentServiceを使って非同期処理を行う」が最高にわかりやすいのでそちらを参照。

ここでは複数のIntentServiceを同期化してみます。
複数のキュー処理を同期化したい、みたいな用途に使えるかもしれません。

まずはIntentServiceその1。
public class XIntentService extends IntentService {
    @SuppressWarnings("unused")
    private static final String TAG = XIntentService.class.getSimpleName();
    private final XIntentService self = this;

    public static final String KEY_ACTION = "action";
    public static final String KEY = "key";
    
    public XIntentService() {
        super(TAG);
    }
    
    public XIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent data) {
        Lock lock = Lock.getInstance();
        synchronized (lock) {

            int value = data.getIntExtra(KEY, -1);
            Log.i(TAG, "onHandleIntent() start  " + value);
            int wait = (int) (Math.random() * 1000);
            try {
                Thread.sleep(wait);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.i(TAG, "onHandleIntent() finish " + value);
        }
    }
}

次にIntentServiceその2。
public class YIntentService extends IntentService {
    @SuppressWarnings("unused")
    private static final String TAG = YIntentService.class.getSimpleName();
    private final YIntentService self = this;

    public static final String KEY_ACTION = "action";
    public static final String KEY = "key";
    
    public YIntentService() {
        super(TAG);
    }
    
    public YIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent data) {
        Lock lock = Lock.getInstance();
        synchronized (lock) {
            
            int value = data.getIntExtra(KEY, -1);
            Log.d(TAG, "onHandleIntent() start  " + value);
            int wait = (int) (Math.random() * 1000);
            try {
                Thread.sleep(wait);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "onHandleIntent() finish " + value);
        }
    }
}
AndroidManifest.xmlにもタグを記述しておくのを忘れずに。 双方のクラスに出てくるLockはロックを提供するシングルトンクラス。
public class Lock {
    @SuppressWarnings("unused")
    private static final String TAG = Lock.class.getSimpleName();
    private final Lock self = this;
    
    private static Lock instance = new Lock();

    private Lock() {}

    public static Lock getInstance() {
      return instance;
    }
}
ここまで来たらあとは適当に呼び出すだけ。
        for ( int i = 0; i < 100; i++ ) {
            Intent intentX = new Intent(this, XIntentService.class);
            intentX.putExtra(XIntentService.KEY_ACTION, "com.awwa.sample.siss.action");
            intentX.putExtra(XIntentService.KEY, i);
            startService(intentX);

            Intent intentY = new Intent(this, YIntentService.class);
            intentY.putExtra(YIntentService.KEY_ACTION, "com.awwa.sample.siss.action");
            intentY.putExtra(YIntentService.KEY, i);
            startService(intentY);
        }
XとYのstartとfinishが入れ子にならないよ!
11-13 06:04:11.437: I/XIntentService(926): onHandleIntent() start  91
11-13 06:04:12.117: I/XIntentService(926): onHandleIntent() finish 91
11-13 06:04:12.124: I/XIntentService(926): onHandleIntent() start  92
11-13 06:04:12.528: I/XIntentService(926): onHandleIntent() finish 92
11-13 06:04:12.528: I/XIntentService(926): onHandleIntent() start  93
11-13 06:04:13.452: I/XIntentService(926): onHandleIntent() finish 93
11-13 06:04:13.452: D/YIntentService(926): onHandleIntent() start  40
11-13 06:04:13.594: D/YIntentService(926): onHandleIntent() finish 40
11-13 06:04:13.595: D/YIntentService(926): onHandleIntent() start  41
11-13 06:04:13.827: D/YIntentService(926): onHandleIntent() finish 41
11-13 06:04:13.835: D/YIntentService(926): onHandleIntent() start  42
11-13 06:04:14.472: D/YIntentService(926): onHandleIntent() finish 42
11-13 06:04:14.472: I/XIntentService(926): onHandleIntent() start  94
11-13 06:04:15.177: I/XIntentService(926): onHandleIntent() finish 94
11-13 06:04:15.177: I/XIntentService(926): onHandleIntent() start  95

コメント

このブログの人気の投稿

Execノードを使う

SendGridのX-SMTPAPIヘッダの使い方(Section Tags、Substitution Tags編)

Joinノードを使う(その4)