在其他线程 Android 中运行的可运行队列
该代码将帮助我解释我的问题:
public class TestHandlerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(){
public void run(){
for (int i=0;i<20;i++){
handler.post(run);
}
}
}.start();
}
Handler handler=new Handler(){
};
Runnable run = new Runnable(){
public void run(){
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("TAG", "Message processed");
}
};
}
这样新线程会发出由主线程中的处理程序提供服务的请愿书。 我需要做的恰恰相反。 UI 线程启动请愿书,并由另一个线程按顺序提供服务(顺序很重要),并且每个请愿书结束时不需要通知 UI 线程。 对此有几点很重要:请愿书有 3 个参数(int、int、boolean),它们在数据库中进行更改,并通过用户与 UI 的交互生成,因此它们不会同时启动 提前致谢
that code will help me explain my problem:
public class TestHandlerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(){
public void run(){
for (int i=0;i<20;i++){
handler.post(run);
}
}
}.start();
}
Handler handler=new Handler(){
};
Runnable run = new Runnable(){
public void run(){
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("TAG", "Message processed");
}
};
}
That way the new thread makes the petitions which are served by the handler in the main thread.
What i need to do is exactly the opposite. The UI thread launches petitions wich are served sequentially by another thread (the order is important), and the UI thread don't need to be notified when each petition end.
Some points are important for this: The petitions have 3 parameters (int, int, boolean), they make changes in the database and are generated by the interaction of the user with the UI, so they aren't launched simultaneously
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是使用它来创建线程:http://developer.android。 com/reference/android/os/HandlerThread.html
这将创建一个具有自己的消息队列和循环的线程。您可以创建一个处理程序来在线程上运行工作,如下所示:
这确实要求线程完成的所有工作都通过处理程序发送消息并在其上调度 Runnables 来完成。
One option is to use this for making your thread: http://developer.android.com/reference/android/os/HandlerThread.html
This will create a thread with its own message queue and loop. You can create a Handler to run work on the thread like so:
This does require that all work done by thread be done so by sending messages and scheduling Runnables on it through Handlers.