- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Android 上是否有任务队列之类的东西?我知道它可以手工编写,但有现成的库吗?
最佳答案
我不确定是否会有这个库,因为 Android 已经为您想要实现的目标提供了高级构建 block 。
如果我对您的理解正确,您希望将任何线程的任务发布到队列中并在专用线程上一个接一个地执行。这就是Android Handler
是为了。
Looper
.Looper
都有一个关联的 MessageQueue
Looper
以线程安全的方式将消息入队和出队到 Looper
的 MessageQueue
。Handler
对象绑定(bind)到同一个 Looper
。如果您想使用不同的处理程序处理不同类型的消息,这将很有用。在这种情况下,您可以保证只有一个处理程序将处理给定 Looper
的 Message/Runnable。 Looper
负责将 Message 分派(dispatch)给正确的 Handler
。golang
的缓冲 channel 模式),那么 Handler
只是一个高级别的让您轻松使用此模式的类。// BEGIN One-time Initialization
// Create a Handler thread
// This provides the looper for the Message Queue and
// will be processing all your messages (i.e. tasks).
handlerThread = new HandlerThread("SomeThreadName");
// Start the Handler Thread
// The thread will block (using the looper) until it
// receives a new message
handlerThread.start();
// Create a Message Handler which you can use to
// post and process messages
// The same Handler can also be used to post a Runnable which will get
// executed on handlerThread
handler = new CustomHandler(mHandlerThread.getLooper());
// END One-time Initialization
// Different ways to post a message to the Handler Thread
// These calls are thread-safe, can be called safely and
// concurrently from multiple threads without race conditions
handler.sendEmptyMessage(MESSAGE_ID_1);
handler.sendEmptyMessage(MESSAGE_ID_2);
handler.sendMessage(handler.obtainMessage(MESSAGE_ID_3, obj1));
handler.sendMessage(handler.obtainMessage(MESSAGE_ID_4, value, obj1));
handler.sendMessage(handler.obtainMessage(MESSAGE_ID_5, value1, valu2, obj1));
// Post a runnable on the Handler Thread
// This is thread-safe as well
// In fact all methods on the Handler class are thread-safe
handler.post(new Runnable() {
@Override
public void run() {
// Code to run on the Handler thread
}
});
// A skeleton implementation for CustomHandler
// NOTE: You can use the Handler class as-is without sub-classing it, if you
// intend to post just Runnables and NOT any messages
public class CustomHandler extends Handler {
public CustomHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message message) {
if (message != null) {
// Process the message
// The result can be sent back to the caller using a callback
// or alternatively, the caller could have passed a Handler
// argument, which the Handler Thread can post a message to
switch (message.what) {
case MESSAGE_ID_1:
// Some logic here
break;
case MESSAGE_ID_2:
// Some logic here
break;
case MESSAGE_ID_3:
// Some logic here
break;
case MESSAGE_ID_4:
// Some logic here
break;
case MESSAGE_ID_5:
// Some logic here
break;
// Add more message types here as required
}
}
}
}
// After you're done processing all messages and you
// want to exit the Handler Thread
// This will ensure that the queue does not accept any
// new messages, and all enqueued messages do get processed
handlerThread.quitSafely();
HandlerThread
,但并不是必须使用它。您甚至可以直接使用 Looper
调用,即 Looper.prepare()
和 Looper.loop()
在线程。Handler
进行子类化。Handler
轻松地在多个线程之间进行通信。Handler
中有一些方法可以安排 future 的消息传递和 Runnable 执行。Android 框架在内部广泛使用 Handler
来管理组件生命周期事件(onPause
、onResume
等)。
AsyncTask是在不同线程上调度任务的另一种选择。 .我不会详细介绍它的实现,因为 Android 开发者文档已经详细描述了它。
我通常将 AsyncTasks 用于我知道我会长时间使用后台线程的任务(至少 >= 100 毫秒)。我能想到的属于这一类的一些例子是 Binder IPC、RPC 调用、网络调用、后台下载等。
另一方面,Handler
更适合专注于尽快处理更多消息的情况。换句话说,避免在 handleMessage()
中执行任何阻塞操作。您可以使用 Handler
轻松编写无锁代码,它为您管理入队和出队消息时的所有锁定。
事实上,AsyncTask
可以与 Handler
结合使用,方法是将工作分成一个快速部分(由 Handler
负责)和一个慢速部分(由 AsyncTask
处理)。
PS:虽然与问题无关,但如果您对消息队列范式感兴趣;看看LMAX Disruptor
,这是一个高性能的线程间消息队列库。他们的设计文档很好地解释了消息队列的哪些部分需要锁定/原子访问。
关于android - Android上的任务队列就像iOS上的GCD一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623266/
环境: Ubuntu 16.04 64 位 g++ 5.4.0 版 这是代码: #include ... auto g = std::gcd(10, 4); ... 我在编译命令中开启了-std=c
我正在尝试用 Python 编写欧几里得算法。它是找到两个非常大的数字的 GCD。公式是 a = bq + r 其中 a 和 b 是你的两个数,q 是 b 均分 a 的次数,r 是余数。 我可以编写代
我正在尝试创建一个处理非常大数字的 gcd 函数。因此,到目前为止我尝试过的任何事情都会导致错误。例如 : fun gcd(a : Int.toLarge, b : Int.toLarge): Int
我是 Haskell 的新手,实际上我才刚开始,我想对我将要问的问题有一点提示。 我目前正在尝试获取给定列表的 GCD。例如,列表 [3, 6, 9] 将返回 3。 目前,我想到了以下方法,我是否朝着
我有一段来自 API 黑暗时代的现有代码。它是一个基于 MPCreateTask 的线程。看起来我可以将其移至 GCG 队列,但有点复杂。当前有三个基于 MPCreateQueue 的队列用于三个优先
出于多种原因,我想让我的应用程序向后兼容 OS X 10.5。 到目前为止,我正在使用 10.6 中添加的大量 GCD 调度队列,如下所示: dispatch_async(dispatch_get_m
我有一个在一些设备上崩溃的 iOS 应用程序。鉴于发生这种情况时我在 iTunes 上看到的差评,崩溃似乎发生在代码中的同一点。 最后,一位好心人实际上联系了我,而不仅仅是留下评论,他们甚至为我安装了
我有一个定期运行的任务,它最初设计为使用 NSThread 和 NSTimer 在与主运行循环不同的单独运行循环上运行。 适应这一点以利用 GCD 的最佳方法是什么? 当前代码: -(void)ini
我想知道这两者之间的性能差异是什么。 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
我研究过 GCD 和线程安全。在苹果文档中,GCD 是线程安全的,即多线程可以访问。而且我了解了线程安全的含义,即每当多个线程访问某个对象时总是给出相同的结果。 我认为 Thread-Safe 和 G
我需要帮助来获取两个输入数字的 GCD。我不知道为什么它不会返回 GCD。例如,55 和 125 的 GCD 将为 5。任何帮助将不胜感激。它必须使用方法,而不是算法。 public static v
几天前我在编程挑战中遇到了这个问题。 在后端的 20 个测试用例中,我只得到了一个通过。这是我的解决方案 import java.util.Scanner; class TestClass {
我研究过 GCD 和线程安全。在苹果文档中,GCD 是线程安全的,即多线程可以访问。而且我了解了线程安全的含义,即每当多个线程访问某个对象时总是给出相同的结果。 我认为 Thread-Safe 和 G
我有一种方法可以帮助我将本地standardUserDefaults与Web服务同步。首先,我需要确保数据已成功同步,然后才能让该方法返回。我目前正在努力解决的问题是,我无法让GCD按特定顺序执行并完
我必须确保 3 个数字之间的 GCD 不大于 1。 这是我迄今为止该方法的代码: private int greatestCommonFactor(int a, int b, int c) {
我有两种在串行队列上运行的方法。每个方法都返回某个类的副本。我试图在保持数据完整性的同时实现线程安全解决方案。 例如: -(Users *) getAllUsers { __block
假设我们有一个 UIVewcontroller,叫它 A,在那个 VC 的 viewdidload 中我们添加两个 UIViewcontrollers(B,C)。现在为了使 A 的 Viewdidlo
我有几个任务被分派(dispatch)到串行队列,特别是一些被分派(dispatch)到组的任务。在调度这些任务后,我想给用户取消它们的选项,即使它们已经被执行。 我找不到任何方法来取消排队的任务,然
因此,我使用 dispatch_async 将 10 个任务放入并发队列中。它们不会阻塞下一个任务,并按顺序处理。我的 UI 具有响应能力。 for (int i = 0; i < 10; i++)
public static int divisor(int m, int n) { if (m == 0 || n == 0) { return m+n; } else
我是一名优秀的程序员,十分优秀!