在Service中的onCreate()中使用synchronized部分

时间:2014-02-19 13:35:34

标签: android

我们需要在服务中同步创建onCreate()中的syncadapter吗?据我所知,系统中只有一个服务实例。因此,并行调用onCreate()没有变化。

来自android developers sample的代码:

public class SyncService extends Service {
    // Storage for an instance of the sync adapter
    private static SyncAdapter sSyncAdapter = null;
    // Object to use as a thread-safe lock
    private static final Object sSyncAdapterLock = new Object();

        public void onCreate() {
        /*
         * Create the sync adapter as a singleton.
         * Set the sync adapter as syncable
         * Disallow parallel syncs
         */
        synchronized (sSyncAdapterLock) {
            if (sSyncAdapter == null) {
                sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
            }
        }
    }
...
}

0 个答案:

没有答案