【问题标题】:When to start separate process in android ?什么时候在android中启动单独的进程?
【发布时间】:2017-03-11 13:13:48
【问题描述】:

我有 100 张图片要从服务器下载,每张图片大小接近 200kb。为了在应用程序中实现此目的,我可能不得不创建 Service OR Intent 服务。我知道意图服务最适合这种情况,因为它在工作线程上运行。但我唯一担心的是,我的应用程序还将启动其他线程以满足不同的需求。所以线程过多可能会导致性能下降。所以我在为意图服务或服务完全分开的过程中做出决定的两难境地。创建单独的流程可能会一直受益。你说什么 ?

【问题讨论】:

    标签: android android-service android-intentservice


    【解决方案1】:

    没关系。如果您还记得您的第一个多线程类,您可能会被告知所有线程不会同时工作,但这就像“一个运行一段时间然后第二次运行然后一个再次运行然后可能第三次等等”顺序是随机的。它不会影响性能,因为对于处理器而言,使用 2 个线程还是 100 个线程并不重要。 但是,请确保您不要等待(例如前 10 个)首先下载图像,这样用户就不必等待太长时间来下载图像或连接缓慢。

    【讨论】:

      【解决方案2】:

      当然,Intent Service 运行在工作线程中,Service 运行在主线程中。所以如果你想在 Service 中下载图片,你仍然需要创建新的 Thread,所以这里的工作是一样的,但是 Intent Service 比 Service 有一些优势:

      1.Background thread ( You no need to create Thread to download images when Service need)
      
      2.Automatic queuing the Intents delivered to onStartCommand(), so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn.
      
      3. the automatic shutdown of the IntentService when job done (in service you need to call stopService)
      

      关于创建新流程是没有必要的。只需创建一个 IntentService 即可下载您的图像。有了更大的数据,您可以创建多个线程并同时下载多个部分,但实际上不需要 100 张图像和 200kb。

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 1970-01-01
        • 2018-05-14
        • 2023-03-13
        • 2015-05-24
        相关资源
        最近更新 更多