线程同步与IntentService [英] Thread Synchronization with IntentService

查看:140
本文介绍了线程同步与IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个应用程序,通过intentservice使得HTTP请求。我需要的应用程序等待服务来完成其运行(即,有请求,在与一些数据被退回),它继续操作之前,作为其业务涉及我希望从HTTP请求接收数据的操作。我试着这样做的许多方法 - 旗语,CountDownLatch,但似乎对所有的人,我需要传递的等待/计数物体插入intentservice,以便它可以告诉主线程,其中的对象是一些方法等待它完成处理。我该如何去这样做呢?基本上,我希望有一个同步的,阻塞到HTTP服务器调用,意图服务方便工作,因为意向服务使多线程容易。

I'm trying to create an app that makes HTTP requests through an intentservice. I need the app to wait for the service to finish its run (aka, have the request be returned with some data) before it continues its operations, as its operations involve manipulation of the data I hope to receive from the HTTP requests. I've tried numerous means of doing so - Semaphore, CountDownLatch, but it seems that for all of them, I need some method of passing in the waiting/counting object into the intentservice so that it can tell the main thread where that object is waiting that it is done processing. How do I go about doing that? Basically, I want a synchronous, blocking call to an http server to work conveniently with an Intent Service, since an intent service makes multi threading easy.

再次重申,只是为了确保我没有滥用术语:我的意思是什么同步和阻塞/我想:我发送的意图,我intentservice发出请求作出HTTP服务器的呼叫。我的UI线程,或线程作为此意图被送往,现在等待直到请求已被处理,并继续运行之前一个结果已经返回。

Again to reiterate just to make sure i'm not misusing terminology: What I mean by Synchronous and blocking/what I want: I make a call to the http server by sending an intent to my intentservice that makes the request. My UI thread, or thread from which this intent was sent, now waits until the request has been processed and a result has been returned before continuing to run.

如果你认为我会这个过程(以阻塞使HTTP调用,同步方式)都错了,什么是你可以选择去做另一种方式?谢谢!

If you think that I am going about this process (making http calls in a blocking, synchronous way) all wrong, what is another way you might choose to go about it? Thanks!

推荐答案

我很抱歉,但我觉得你的架构是不正确的或者我可能理解错了。 IntentService是建立在做单独的线程的事情串行方式。现在你说你希望它是同步和阻塞。你不能阻止UI线程!

I am sorry, but I think your architecture is not right or I may understand it wrong. IntentService is built to do thing serial way on separate thread. Now you say you want it to be synchronous and blocking. You cannot block UI thread!

为了从您的IntentService到活动/片段/等创建通知系统。你有几种选择:单,广播消息(接收,resultReceiver),其他

In order to create notification system from your IntentService to Activity/Fragment/etc. you have few choices: singleton, broadcast message (receiver, resultReceiver), others?

上假设的服务和应用程序的其他部分在相同的过程中工作的基础。最好的办法是创造经理做好这项工作。像这样的东西可建启动服务以及侦听完成事件:

Based on assumption that service and other parts of the application are working in same process. Best option would be to create manager to do this job. Something like this can be built to start service as well as listen for completion event:

public class MyNetworkManager {

    static MyNetworkManager sInstance;
    Context mContext;
    LinkedList<OnCompletionListener> mListeners;

    private MyNetworkManager(Context context) {
        mContext = context;
        mListeners = new LinkedList<>();
    }

    public static MyNetworkManager getInstance(Context context) {
        if (sInstance == null) {
            synchronized (MyNetworkManager.class) {
                if (sInstance == null) {
                    sInstance = new MyNetworkManager(context.getApplicationContext());
                }
            }
        }
        return sInstance;
    }

    // add listener to listen for completion event
    public void addListener(OnCompletionListener listener) {
        synchronized (mListeners) {
            mListeners.add(listener);
        }
    }

    // remove listener to stop listening for completion event
    public void removeListener(OnCompletionListener listener) {
        synchronized (mListeners) {
            mListeners.remove(listener);
        }
    }

    // call from UI to start service operation
    public void startNetworkOperation() {
        Intent service = new Intent();
        mContext.startService(service);
    }

    // call from service to notify UI (still on different thread, can use Handler to make call on main thread)
    public void notifyCompletion() {
        synchronized (mListeners) {
            for (OnCompletionListener listener : mListeners) {
                listener.onCompleted(this);
            }
        }
    }

    public static interface OnCompletionListener {

        void onCompleted(MyNetworkManager manager);
    }
}

这篇关于线程同步与IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆