0

When reading Android's documentation on threads and processes. The documentation is too ambiguous they say that it runs under a single thread called main but not sure whether it's single threaded per process or across processes.

I'm trying to understand the thread creation in my phone. I just created two sample apps and tried to print the current thread id and thread name using Thread.currentThread().getId() and Thread.currentThread().getName in both the apps the thread name was main and the thread id is 2. Similarly I tried printing process id for individual app with android.os.Process.myPid() - the process id were different. So it's obvious that it's creating new process for each application.

I'm doubtful whether it's the same thread used for rendering the activity between Application1 and Application2. If yes, how this is handled by Android framework? If it's a single thread for the entire device (phone) then what happens when I invoke Thread.currentThread().sleep(1000 * 60 * 60) in one application and try to launch another activity from Application2 from adb shell command. Will this cause any exception here?

Is there any thread pool associated with a process - in the case of Android when it creates a new process for an Application?

As per my understanding, zygote process is cloned during each application process is getting created (the parent process would be zygote)? Is zygote creating a main thread which all the applications are reusing?

How does process and threads are associated with each other? Can parent and child process share the threads?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63

2 Answers2

1

Threads are sub-object of processes.

Each process can have one or more threads, and each has it's own memory space.

Threads on different processes are different threads.

You can read more about the differences between them here: What is the difference between a process and a thread?

gkpln3
  • 1,317
  • 10
  • 24
  • Then why do they really have the same thread id even when i kill and run the app multiple times? – Tom Taylor Dec 11 '19 at 16:08
  • @rm-rfstar Once you start a new process, it gets loaded into a new environment. It's thread count start from 0 again. – gkpln3 Dec 11 '19 at 16:17
1

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).

This and more on this topic can be found in the official documantation here: Android processes and threads overview

From myself I can add, that same application components share the same process and main thread, unless they are especially registered in AndroidManifest.xml to run in a separate process. Nevertheless, when a new process is started, Android will create and start few threads, one of which is the main/UI thread, it will have same id - 2, since it's the 3rd to be started by the system.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • I'm little confused with the last line. Since it's the 3rd to be started by the system why does it hold the id 2? And what are first two threads? – Tom Taylor Dec 11 '19 at 17:38
  • 1
    @rm-rfstar thread count starts from 0, so if it's 2, it's basically 3rd in the row. Sometimes, it might be 1, meaning that it's 2nd.. What are the others I do not know, but I might guess they are responsible for some service backup work or error handling (showing ANR window, etc.) – Sergey Emeliyanov Dec 12 '19 at 01:33