Android 线程 - 延迟重复线程组几次

发布于 2024-11-03 14:17:45 字数 842 浏览 0 评论 0原文

我将实现一个从 Android 设备收集数据的应用程序。

这是我现在所拥有的:

  • 带有按钮的主要活动运行
  • 按钮启动记录器
  • 记录器有4个资源 可运行
    • Gps资源
    • 图像资源
    • 音频资源
    • 陀螺仪资源
  • Recorder中,有一个for循环(例如i <3),它运行线程包一些延迟 (i * 5000)

我应该如何创建该线程,以便所有线程同时启动 (i=0),然后在 5 秒后再次启动 (i=1) 并在接下来的 5 秒后再次 (i=2) ?

接下来的事情:

每个资源都使用 getData() 方法返回一些数据 - 简化它 - 带有随机字符的字符串。如何在Recorder中通知队列中的所有线程都已完成并从资源中收集所有数据?

最后一件事:

我必须能够在创建所有队列后停止执行所有线程。示例:

我们有第 7 秒,第二个队列正在运行,用户单击按钮停止。正在运行的线程的队列即将完成,但下一个队列将不会启动,只是 Recodred 必须忘记它们。

我尽量写得简单一些,相信你们能理解我的意思。

感谢您的任何建议!

I am going to implement an application that is gathering data from Android device.

Here is what I have right now:

  • Main activity with a button Run
  • Button starts a Recorder
  • Recorder has 4 Resources that are Runnable
    • GpsResource
    • ImageResource
    • AudioResource
    • GyroscopeResource
  • In Recorder there is a for loop (for example i < 3) that runs pack of threads with some delay (i * 5000)

How should I create that threads so all of them will start simultaneously (i=0), then after 5 seconds start again (i=1) and after next 5 seconds again (i=2) ?

Next thing:

Every resource returns some data with getData() method - simplify it - string with random characters. How to notify in Recorder that all threads in queue are completed and gather all data from resources ?

The last thing:

I have to be able after creating all that queues to stop executing all threads. Example:

We have a 7th second, 2nd queue is running now and user clicks a button Stop. Queue with running threads is going to complete, but next queues will not start, just Recodred has to forget about them.

I tried to write as simple as possible, I believe that you guys understand me.

Thanks for any advices !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2024-11-10 14:17:45

这里有一个想法可以解决至少一部分问题:

一起启动资源并等待它们完成:

采用 CompletionService

Executor e = Executors.newFixedThreadPool(4);
CompletionService<String> service = new ExecutorCompletionService<String>(e);
CountDownLatch startLatch = new CountDownLatch(1);
for (int i=0; i<numberOfResources; i++) {
     Resource r = createResource(startLatch, i);
     service.submit(r);
}
startLatch.countDown();

Resource 应该如下所示:

class Resource implements Callable<String> { 
    private final CountDownLatch latch;
    public Resource(CountDownLatch latch) { this.latch = latch; }
    public String call() throws Exception {
        latch.await();
        return getData();
    }
}

不从完成服务中获取数据:

for (int i=0; i<numberOfResources; i++) {
    String data = service.take().get();
    // do something with data
}

请注意,这更多是伪代码,未经过实际测试:)

并且仍然存在悬而未决的问题,如果某些资源执行时间超过 5 秒,您会做什么。另一种是每 5 秒安排一次相同的操作。

Here is an idea to solve at least one part of the question:

To start resources together and wait for them to complete:

Take a CompletionService:

Executor e = Executors.newFixedThreadPool(4);
CompletionService<String> service = new ExecutorCompletionService<String>(e);
CountDownLatch startLatch = new CountDownLatch(1);
for (int i=0; i<numberOfResources; i++) {
     Resource r = createResource(startLatch, i);
     service.submit(r);
}
startLatch.countDown();

Resource should look something like this:

class Resource implements Callable<String> { 
    private final CountDownLatch latch;
    public Resource(CountDownLatch latch) { this.latch = latch; }
    public String call() throws Exception {
        latch.await();
        return getData();
    }
}

Not to get the data back from the completion service:

for (int i=0; i<numberOfResources; i++) {
    String data = service.take().get();
    // do something with data
}

Please note that this is more of pseudo-code, not actually tested :)

And there are still open questions what would you do if some resource takes more than 5 seconds to execute. Another one is to schedule the same operations every 5 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文