在Android中多线程是如何工作的?

时间:2022-06-29 20:59:40

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

我正在尝试并行运行4个线程,但是失败了。

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

我尝试过使用同步等待()和notify()循环障碍ThreadPoolExecutor CountDownLatch asynctask和许多其他的,但并没有管理线程并行运行。

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

我们能同时运行4个线程吗?如何?

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

注意:我使用的是音频流,使用的是AudioRecord和AudioTrack API。

3 个解决方案

#1


14  

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

我们可以同时运行4个线程并行吗?

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

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

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?

问题:我不能运行线程并行,即第一个线程运行:4次,第2次,第3次,第4次,只有1次。如何使它运行(尝试使用前面提到的线程但不成功地运行所有线程并行)?

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

如果您的目标只是让所有线程都积极地进行处理,那么我建议您查看Java线程池(以及更一般的执行器)。

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

android支持多线程?如果是,在android中多线程是如何工作的?

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

是的,Android支持多线程。实现它(除了线程池和执行程序之外)最理想的方式是AsyncTask

AsyncTask 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.

AsyncTask允许你实现doInBackground(),在那里你的线程可以处理它的任务。这与您从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.

真正的奇迹发生在您重写onPreExecute()和onPostExecute()时,它们都在UI线程上执行。这将阻止您获取关于您的活动没有被附加的消息。

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

这个答案包含一个用于AsyncTask的小代码示例,可以帮助您开始。

Also, have a look at Handlers in Android

还有,看看Android中的处理器

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

编辑——我在研究中发现,录音、发送和接收音频都是非常交互的过程,它们需要同步和IO资源。最好的解决方案可能是,您可以为每个操作创建单独的AsyncTask,以便每个操作在其单独的AsyncTask中执行。

#2


2  

  1. You can run 4 different Threads, but in order get executed in parallel (simultaneously) you arch have to support it. If you have a single core cpu, the order which threads are excuted is up to the scheduler and only one thread at time will be executed
  2. 您可以运行4个不同的线程,但是为了并行地(同时)执行,arch必须支持它。如果您只有一个核心cpu,那么线程执行的顺序取决于调度程序,并且每次只执行一个线程
  3. The order and the timing of the execution are up to the scheduler.
  4. 执行的顺序和时间由调度程序决定。
  5. yes it does. You can rely both on AsyncTask or the java features (Executors eg)
  6. 的确是的。您可以同时依赖于AsyncTask或java特性(Executors,如)

#3


-4  

You can use Async Task to run them in parallel.

您可以使用Async任务并行地运行它们。

#1


14  

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

我们可以同时运行4个线程并行吗?

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

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

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?

问题:我不能运行线程并行,即第一个线程运行:4次,第2次,第3次,第4次,只有1次。如何使它运行(尝试使用前面提到的线程但不成功地运行所有线程并行)?

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

如果您的目标只是让所有线程都积极地进行处理,那么我建议您查看Java线程池(以及更一般的执行器)。

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

android支持多线程?如果是,在android中多线程是如何工作的?

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

是的,Android支持多线程。实现它(除了线程池和执行程序之外)最理想的方式是AsyncTask

AsyncTask 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.

AsyncTask允许你实现doInBackground(),在那里你的线程可以处理它的任务。这与您从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.

真正的奇迹发生在您重写onPreExecute()和onPostExecute()时,它们都在UI线程上执行。这将阻止您获取关于您的活动没有被附加的消息。

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

这个答案包含一个用于AsyncTask的小代码示例,可以帮助您开始。

Also, have a look at Handlers in Android

还有,看看Android中的处理器

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

编辑——我在研究中发现,录音、发送和接收音频都是非常交互的过程,它们需要同步和IO资源。最好的解决方案可能是,您可以为每个操作创建单独的AsyncTask,以便每个操作在其单独的AsyncTask中执行。

#2


2  

  1. You can run 4 different Threads, but in order get executed in parallel (simultaneously) you arch have to support it. If you have a single core cpu, the order which threads are excuted is up to the scheduler and only one thread at time will be executed
  2. 您可以运行4个不同的线程,但是为了并行地(同时)执行,arch必须支持它。如果您只有一个核心cpu,那么线程执行的顺序取决于调度程序,并且每次只执行一个线程
  3. The order and the timing of the execution are up to the scheduler.
  4. 执行的顺序和时间由调度程序决定。
  5. yes it does. You can rely both on AsyncTask or the java features (Executors eg)
  6. 的确是的。您可以同时依赖于AsyncTask或java特性(Executors,如)

#3


-4  

You can use Async Task to run them in parallel.

您可以使用Async任务并行地运行它们。