How does multi-threading work in Android?

前端 未结 2 1010
醉酒成梦
醉酒成梦 2021-02-19 20:18

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

I have tried using synchronous wait() and notify() CyclicBarrier Thre

相关标签:
2条回答
  • 2021-02-19 21:14
    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. The order and the timing of the execution are up to the scheduler.
    3. yes it does. You can rely both on AsyncTask or the java features (Executors eg)
    0 讨论(0)
  • 2021-02-19 21:20

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

    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.

    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

    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.

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

    0 讨论(0)
提交回复
热议问题