IntentService的实现使用的LinkedBlockingQueue? [英] Implementation of IntentService using LinkedBlockingQueue?
问题描述
我试图下载使用IntentService多个文件。该IntentService donwloads他们欧凯预期一次,唯一的问题是,当互联网下跌的意图服务将不会停止donwload而是将卡住在当前线程上。如果我设法阻止当前线程,将继续运行存储在其队列中的其它线程即使网络连接已断开。
I'm trying to download multiple files using IntentService. The IntentService donwloads them okey as expected one at a time, the only problem is that when the Internet is down the intent service will not stop the donwload rather it will get stuck on the current thread. If I manage to stop the current thread it will continue running the other threads stored in its queue even though the internet connection is down.
有人建议在我使用的LinkedBlockingQueue,并创建自己的工作线程不断检查这个队列新主题另一篇文章。现在我知道了创建和销毁线程的时候有一些增加的开销,因此性能问题,但是这并不在我的情况令人担忧。
It was suggested in another post that I use LinkedBlockingQueue and create my own Worker thread that constantly checks this queue for new threads. Now I know there are some increased overheads and thus performance issues when creating and destroying threads but that's not a concern in my case.
在这一点上,所有我想要做的是了解IntentService如何运作其中作为然而我没有(我已经看过了code),然后拿出我自己的实现它使用的LinkedBlockingQueue控制由一个工作线程。以前有没有人这样做呢?可以提供一个工作的例子,如果你觉得不舒服提供源$ C $ C,伪code是罚款由我。谢谢!
At this point, All I want to do is understand how IntentService works which as of yet I don't (and I have looked at the code) and then come up with my own implementation for it using LinkedBlockingQueue controlled by a Worker thread. Has anyone done this before ? Could provide a working example, if you feel uncomfortable providing the source code, pseudo code is fine by me. Thanks!
更新:我最终使用一个线程,有一个弯,它检查这又店的意图从startService(意向)
UPDATE: I eventually implemented my own Intent Service using a thread that has a looper which checks the queue which in turn stores the intents passed from the startService(intent).
public class MyIntentService extends Service {
private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>();
public MyIntentService(){
super();
}
@Override
public void onCreate() {
super.onCreate();
new Thread(queueController).start();
Log.e("onCreate","onCreate is running again");
}
boolean killed = false;
Runnable queueController = new Runnable() {
public void run() {
while (true) {
try {
Download d =queue.take();
if (killed) {
break;
}
else {
d.downloadFile();
Log.e("QueueInfo","queue size: " + queue.size());
}
}
catch (InterruptedException e) {
break;
}
}
Log.e("queueController", "queueController has finished processing");
Log.e("QueueInfo","queue size: " + queue.toString());
}
};
class Download {
String name;
//Download files process
void downloadFile() {
//Download code here
}
Log.e("Download","Download being processed is: " + name);
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
}
public void killService(){
killed = true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Download d = new Download();
d.setName(intent.getStringExtra("VIDEOS"));
queue.add(d);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("stopSelf","stopSelf has been just called to stop the Service");
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我不那么肯定了START_NOT_STICKY在onStartCommand()方法。如果这是正确的标志返回与否。上任何澄清将AP preciated!
I'm not so sure about the START_NOT_STICKY in the onStartCommand() method. If it's the right flag to return or not. Any clarification on that would be appreciated!
推荐答案
更新:我最终使用一个线程,有一个弯,它检查反过来存储来自startService通过了意图队列(意向)来实现自己的意图服务
UPDATE: I eventually implemented my own Intent Service using a thread that has a looper which checks the queue which in turn stores the intents passed from the startService(intent).
公共类MyIntentService延伸服务{
public class MyIntentService extends Service {
private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>();
public MyIntentService(){
super();
}
@Override
public void onCreate() {
super.onCreate();
new Thread(queueController).start();
Log.e("onCreate","onCreate is running again");
}
boolean killed = false;
Runnable queueController = new Runnable() {
public void run() {
while (true) {
try {
Download d =queue.take();
if (killed) {
break;
}
else {
d.downloadFile();
Log.e("QueueInfo","queue size: " + queue.size());
}
}
catch (InterruptedException e) {
break;
}
}
Log.e("queueController", "queueController has finished processing");
Log.e("QueueInfo","queue size: " + queue.toString());
}
};
class Download {
String name;
//Download files process
void downloadFile() {
//Download code here
}
Log.e("Download","Download being processed is: " + name);
}
public void setName(String n){
name = n;
}
这篇关于IntentService的实现使用的LinkedBlockingQueue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!