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

问题描述

我编码,其中远程服务必须在所有时间运行,并执行这些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,子类的秒;这些线程将阻止他们的的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.

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

08-20 06:39