Java Interview : Whats a threads and a process in Java?
All components of the same application run in the same process and most applications does not change this state.When an application is launched, the system creates a thread of execution for the application, as in Java we call “main.”
Process is a executing program.Threads run in context of a process.Threads are the smallest unit to which processor time is allocated by the operating system. A process may also be made up of multiple threads of execution that execute instructions concurrently.
Threads run in shared memory, where as processes run in separate memory space.Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space.
Threads are easier to create than processes since they don’t require a separate address space. Threads are considered lightweight because they use far less resources than processes.
A process has a fixed security token. A thread, on the other hand, can impersonate different users/tokens.
So from the Microsoft docs,
“Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread’s set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread’s process. Threads can also have their own security context, which can be used for impersonating clients.”
Comments
Post a Comment