如何多线程工作,A​​ndroid的?多线程、工作、ndroid

2023-09-13 01:34:56 作者:花落べ谁伶惜"

我想并行运行4个线程,但失败了。

我一直使用尝试同步等待()通知() 的CyclicBarrier 的ThreadPoolExecutor CountDownLatch 的AsyncTask 和其他许多人,但没有管理并行运行的线程。

我们可以并行运行4个线程(即在同一时间在机器人)?怎么样?

注:我与音频流使用工作 AudioRecord AudioTrack API的

解决方案   

我们能运行四线程并行,即在同一时间在安卓??

是的,你可以运行并行4线程。添加更多的线程通常会有所帮助,但在某些时候,它们会导致一些性能下降。但4个线程不会造成问题。

  

问题:我不能运行的线程并行,即第一线程运行:   4次,第二次第三次ñ4日只有1次。如何使它运行(通过尝试   上述线程,但不成功运行的所有线程并行)并行?

如果你的目标仅仅是让所有线程积极处理,我建议在看的Java 线程池(和执行人更普遍)。

  

Android的支持多线程?如果是的话怎么多线程工程   安卓??

是,Android支持多线程。实现它的最preferred方式(除线程池,执行器)是 AsyncTask的

AsyncTasc 允许您执行 doInBackground(),你的线程可以在其任务杀青了。这是类似的功能,你会得到从

真正神奇的,当你重写发生在preExecute() onPostExecute(),这两者都是在UI线程上执行。这应该让你获得你的活动不被附在邮件。

这answer包含的AsyncTask 小code的例子,可以让你开始。

另外,看看处理程序在安卓

修改 - 什么我的研究过程中,我发现的是,录音,发送和接收音频的所有都相当交互式preocesses,而且需要同步和IO资源。它的最好的解决办法是,你可以创建单独的异步任务为每个操作的,这样在其独立的异步任务执行的每项操作。

I am trying to run 4 threads in parallel, but failing.

I have tried using synchronous wait() and notify() CyclicBarrier ThreadPoolExecutor CountDownLatch AsyncTaskand many others, but didn't manage running the threads in parallel.

Can we run 4 threads in parallel (i.e. at same time in android)? How?

Note: I am working with Audio Streaming using AudioRecord and AudioTrack API's.

解决方案

Can we run 4 thread parallel i.e. at same time in android ??

Yes, you can run 4 thread in parallel. Adding more threads usually helps, but after some point, they cause some performance degradation. but 4 threads will not cause an issue.

Issues : I am not able to run thread parallel i.e. 1st Threads runs : 4 times, 2nd 3rd n 4th only 1 time. How to make it run (Tried using mentioned threads but unsuccess to run all thread parallel) parallel ?

If your goal is simply to have all threads actively processing, I suggest looking at Java thread pools (and Executors more generally).

android support MULTI-THREADING ? If yes how multi-threading works in android??

Yes, Android supports Multithreading. The most preferred way of implementing it(other than Thread Pool and Executor) is AsyncTask

AsyncTasc allows you to implement doInBackground(), where your thread can crank away at its task. This is similar to the functionality you'd get from Thread.

The real magic happens when you override onPreExecute() and onPostExecute(), which are both executed on the UI thread. This should keep you from getting messages about your Activity not being attached.

this answer contains a small code example for AsyncTask that could get you started.

Also, have a look at Handlers in Android

Edit- What I found during my research is that, recording, sending and receiving audio all are quite interactive preocesses and they require sync and IO resources. The best possible solution for it could be, you can create separate Async Task for each of the operation, so that each operation is performed in its separate Async Task.

 
精彩推荐
图片推荐