简体   繁体   English

Android:连续SQL插入的阻塞或非阻塞队列?

[英]Android: blocking or non-blocking queue for continuous SQL inserts?

I have an app where upon pressing a Start button, a service will begin that polls a few sensors, stores the sensor data into some object whenever the sensor values change. 我有一个应用程序,在其中按下“开始”按钮时,将启动一项服务,该服务将轮询几个传感器,并在传感器值发生变化时将传感器数据存储到某个对象中。 Every 10ms, a database insert occurs that takes the objects current values and stores them into the database. 每隔10毫秒,就会发生一次数据库插入操作,该操作将获取对象的当前值并将其存储到数据库中。 This happens for 30 minutes 这发生了30分钟

Given the speed and duration of insertion, I want to run this in a separate thread from the UI thread so navigation doesn't take a hit. 考虑到插入的速度和持续时间,我想在与UI线程不同的线程中运行此命令,以便导航不会受到影响。 So my service will offer some data to the thread by adding it to a queue, then the other thread (consumer) will take from the queue and insert into the database 因此,我的服务将通过将一些数据添加到队列中来向线程提供一些数据,然后另一个线程(消费者)将从队列中取出并插入数据库中

When the Stop button is pressed, I need to make sure to process the rest of the queue before killing off the thread. 当按下“停止”按钮时,我需要确保在终止线程之前处理其余队列。

It seems that everywhere I look, some sort of blocking queue is recommended for producer/consumer type situations (eg LinkedBlockingQueue vs ConcurrentLinkedQueue , or What's the different between LinkedBlockingQueue and ConcurrentLinkedQueue? ) 在我看来,似乎到处都是针对生产者/消费者类型的情况建议使用某种阻塞队列(例如, LinkedBlockingQueue与ConcurrentLinkedQueue ,或者LinkedBlockingQueue和ConcurrentLinkedQueue有什么区别?

My question is, does a blocking queue make sense in my situation? 我的问题是,在我的情况下,阻塞队列是否有意义?

The most vital thing in this app is that all data gets inserted into the db. 此应用程序中重要的事情是所有数据都已插入到数据库中。 From what I understand (please correct me if I'm wrong), but if the queue becomes full, and the consumer thread can't do inserts quickly enough to free up more queue space, then the producer is blocked from adding things to the queue? 据我了解(如果我错了,请纠正我),但是如果队列已满,并且使用者线程不能足够快地进行插入以释放更多的队列空间,那么生产者就无法向其中添加内容。队列? If that's right then by the time queue the has free space, a few sensor readings would have gone by and they would not be inserted into the db due to the blocking 如果这是正确的,那么在时间队列中有可用空间,一些传感器读数将过去,并且由于阻塞,它们将不会插入数据库中。

At the end of the day, I just need the best way to ensure that data gets inserted every 10ms without skipping a beat. 归根结底,我只需要最好的方法来确保每10毫秒插入一次数据,而不会跳动。 In my mind it makes sense to dump the values into some unbounded, limitless queue every 10ms, and have the consumer poll it as soon as it's able. 在我看来,每10毫秒将值转储到一个无限制的无限队列中是有意义的,并让消费者尽快对其进行轮询。 Then when Stop is pressed, drain the rest of the queue before killing the thread. 然后,当按下Stop键时,请先清空其余队列,然后再终止线程。

So what is the correct way to handle this in a 1 producer/1 consumer situations? 那么在1个生产者/ 1个消费者的情况下处理此问题的正确方法是什么?

If I were you, I would use a single thread executor for this task - it already comes with the exact functionality that you need out of the box. 如果您是我,那么我将使用单个线程执行程序来执行此任务-它已经具有开箱即用所需的确切功能。 More info here . 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM