开发者

Can I create a loop with a max amount of operations at a time?

In Java (Android), is there a way to create a loop that go开发者_开发知识库es through a lot of operations, but only is capable of doing n amount of operations at a time?

For example, let's say I need to make 50 network operations, but only want to run, say, ten at a time, and queue the rest?

Cheers

EDIT: This is already fixed. I found a solution for my problem, but cannot close the question yet. Sorry!


This seems like more of a design issue than anything else. My current project requires a similar solution, and we decided to use a bounded ExecutorService to solve the problem. It may not be suitable for your particular issue, but it's worth checking out. I am also not up to speed on Android development unfortunately, so I'm not sure whether it would be a suitable solution, but check this thread.


It sound's like you're looking for semaphores. (Basically a sort of lock, where you permit multiple threads to execute a piece of code, but no more than the allotted amount at any given time, sort of like if you have 5 bathrooms, and 10 people needing to use them, you would let the first 5 in, and the renaming 5 as space cleared up). And it looks like they're built into Java.


I managed to solve my problem using a method and some checks in it. It's kinda hard to explain, as I've made some custom stuff in the classes I'm using, but the important thing is that I solved it :)


implement Runnable and assign an ID field to each object which will be the for loop index the loop queue 10 runnables and pauses check id while executing Runnable and turn a flag on

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜