如何从分离线程初始化处理程序? [英] How to initialize handlers from separated threads?

查看:121
本文介绍了如何从分离线程初始化处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码,其中远程服务必须在所有时间运行,并执行这些TAKS应用程序:

I am coding an application where a remote service has to run at all time and to perform these taks :


  • 创建并保持一个蓝牙连接到其他设备

  • 问这个设备的信息定期(1秒)

  • 获取GPS定位周期(1秒)

  • 在一个文本文件中写入previous DATAS每1秒

对于这一点,我从远程服务2个线程创建的。该loopThread,从蓝牙设备得到DATAS后应该问gpsThread的位置。它必须是非常快的,这就是为什么我使用一个线程,这样我就可以在位置存储在其中可以发送一个变量。

For this, I created from my remote service 2 Threads : one for the data request (loopThread) and one for the GPS Location (gpsThread). The loopThread, after getting the datas from the blueTooth Device should ask the gpsThread for the location. It has to be very quick, that's why I am using a Thread, so i can store the Location in a variable which can be sent.

远程serviceand 2个线程应通过沟通处理。

The remote serviceand the 2 threads should communicate through handlers.

的问题是:我可以使每个处理程序与远程服务进行通信,但不与彼此

The problem is : I can make each Handlers communicate with the remote service, but not with each other.

我创建这样的主题:

myGPSThread = new GPSThread(mainServiceHandler,locationManager);
myLoopThread = new AcquisitionThread(mainServiceHandler, sockIn, sockOut);

我试图通过短信发送一个处理程序给对方,但处理程序似乎并不parcelable。

I tried sending the Handler of one to the other by message, but Handlers seem not to be parcelable.

有没有人有解决这个?

推荐答案

如果你要坚持你的处理程序为基础的方法,你可以设置你的两个主题取值如下:

If you want to stick to your Handler based approach, you can set up your two Threads as follows.

有关你的 S,子类的的>,而不是主题。此外,请他们实施 Handler.Callback 不要开始()他们的时候了。

For your Threads, subclass HandlerThread instead of Thread. Also, make them implement Handler.Callback and don't start() them right away.

final class GPSThread extends HandlerThread implements Handler.Callback {
    private Handler otherThreadHandler;
    public void setOtherThreadHandler(Handler otherThreadHandler) {
        this.otherThreadHandler = otherThreadHandler;
    }
    @Override
    public void handleMessage(Message msg) {
        // like in your comment
    }

}

myGPSThread  = new GPSThread(locationManager);
myLoopThread = new AcquisitionThread(sockIn, sockOut);
myGPSThreadHandler  = new Handler(myGPSThread.getLooper(), myGPSThread);
myLoopThreadHandler = new Handler(myLoopThread.getLooper(), myLoopThread);
myGPSThread.setOtherThreadHandler(myLoopThreadHandler);
myLoopThread.setOtherThreadHandler(myGPSThreadHanlder);
myGPSThread.start();
myLoopThread.start();

如果你想低延迟和你的事件驱动code是短暂而友好的,你可能想用优于默认创建 HandlerThread 取值优先;看到 href=\"http://stackoverflow.com/a/14217816/1856738\">。

If you want low latency and your event-driven code is short and friendly, you may want to create the HandlerThreads with a better-than-default priority; see here.

如前所述,可以同时设置这两个的发 S =htt​​p://developer.android.com /reference/java/util/concurrent/LinkedBlockingQueue.html相对=nofollow>的LinkedBlockingQueue 秒;这些线程将阻止他们的的run()在从另一个线程等待一个消息(又名对象)的方法。

As already mentioned, you can as well set up two "ordinary" Threads which operate on two LinkedBlockingQueues; these Threads would block in their run() methods upon waiting for a message (aka Object) from the other Thread.

这篇关于如何从分离线程初始化处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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