Android的线程池来管理多个蓝牙HANDELING线程? [英] Android Thread Pool to manage multiple bluetooth handeling threads?

查看:110
本文介绍了Android的线程池来管理多个蓝牙HANDELING线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有有它的主机和客户端我的Andr​​oid蓝牙应用。问题是,因为我正在多个连接,我需要一个线程来处理每个连接。这是所有milk'n'cookies,所以我想我会坚持所有线程在数组中。一个小小的研究说要这样做更好的方法是使用一个线程池,但我似乎无法让我的头围绕如何工作的。此外,它实际上甚至可以在一个数组来保存线程?

So I have my Android Bluetooth application that has it's host and clients. The problem is, because I am making multiple connections, I need a thread to handle each connection. That's all milk'n'cookies, so I thought I'd stick all the threads in an array. A little research says a better method to doing this is using a Thread Pool, but I can't seem to get my head around how that works. Also, is it actually even possible to hold threads in an array?

推荐答案

一个线程池周围的想法,因为一遍一遍创建线程是耗时的建成,我们应该尝试尽可能多地回收。因此,一个线程池是执行​​的作业,但是,当他们完成工作不被破坏,而是回归池的线程collection,要么采取另外的工作或处于闲置状态,如果没有什么可以做的。

A thread pool is built around the idea that, since creating threads over and over again is time-consuming, we should try to recycle them as much as possible. Thus, a thread pool is a collection of threads that execute jobs, but are not destroyed when they finish a job, but instead "return to the pool" and either take another job or sit idle if there is nothing to do.

通常情况下,其实现是线程安全的队列中,程序员把工作和一堆被执行管理线程保持轮询(我没有暗示忙着纺必然)队列工作。

Usually the underlying implementation is a thread-safe queue in which the programmer puts jobs and a bunch of threads managed by the implementation keep polling (I'm not implying busy-spinning necessarily) the queue for work.

在Java的线程池再由的ExecutorService 类可以是psented $ P $:

In Java the thread pool is represented by the ExecutorService class which can be:


  • 固定 - 创建一个线程池有固定数量的线程

  • 缓存 - 动态地创建和销毁线程需要

  • 单 - 使用一个线程池

请注意,由于线程池中的线程以上(即再循环),在一个固定的线程池的情况下,不建议所描述的方式运作有那些阻塞I / O操作的工作,因为线程采取这些工作将从池中被有效地去除,直到他们完成这项工作,因此你可能有死锁。

Note that, since thread pool threads operate in the manner described above (i.e. are recycled), in the case of a fixed thread pool it is not recommended to have jobs that do blocking I/O operations, since the threads taking those jobs will be effectively removed from the pool until they finish the job and thus you may have deadlocks.

至于线程的阵列,它的简单,只要创建任何对象数组:

As for the array of threads, it's as simple as creating any object array:

Thread[] threads = new Thread[10]; // array of 10 threads

这篇关于Android的线程池来管理多个蓝牙HANDELING线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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