Skip to main content

Posts

Threads and Processes: What's the difference?

A Process: A process is the carrying out of a program that allows you to perform the program's actions specified. The operating system helps you to schedule, create, and terminate the processes that are used by the CPU. When a process is loaded into memory, it contains all the resources required to perform the actions necessary. These resources include mainly registers, a program counter and a stack. The register holds the data, such as instructions needed by the Process, in the CPU. There is a program counter to keep track of where the computer program is with completing the actions. Finally, a stack holds the data of the active subroutines of the program. A Thread: A Thread is a unit of execution that forms part of a process. A Thread cannot contain a Process. However, a Process can have multiple Threads that are performed at relatively the same time. Threads are lightweight, as they have their own stack and are self-sufficient. Unlike Processes, if there is a problem with one of...

Different States of a Thread:

Different states of a Thread:  Threads are in a new state when they are created. In this state, the code has yet to be executed. Runnable State:  A Thread is moved to a Runnable State when it is ready to execute. In this state, a Thread might be running, or it might be prepared to run whenever there is a place to, organized by the Thread Scheduler. In a Multi-Thread process, a specific amount of time is allocated to each Thread. Each Thread gets a specific allocated timeframe to run before switching to a new Thread, giving each equal opportunity to execute the actions. When this happens, all Threads running, waiting to run, or waiting for the CPU, lie in a Runnable State. Blocked/Waiting state: When a Thread is inactive for a brief time, it is said to be in one of these states: Blocked Waiting The Thread Scheduler is responsible for reactivating and scheduling the blocked/waiting Thread back to a Runnable State. A Thread will not be able to continue until it has been moved bac...

Daemon Threads:

Daemon Threads: Generally speaking, daemon threads are for background tasks, and non-daemon (user-threads) threads are for foreground tasks. The only difference is that JVM shuts down when the last non-daemon Thread terminates. More often than not, background tasks run at a lower priority than foreground tasks (e.g. garbage collection). Still, many background tasks run at a higher priority (tracking the mouse etc.). When an application starts, the main Thread (the Thread that calls the application's "main" method) is the only non-daemon Thread. When the application's main method exits, the main Thread terminates. If no other non-daemon threads are running, the application exits.  Servers typically have a non-daemon thread listening for client connections and then use non-daemon threads for processing client requests. When the last client request thread closes, the connection listener thread is terminated to shut the server down.  How to create a Daemon Thread: This is...

Jave Memory Models (JMM):

What is the Java Memory Model? The Java memory model specifies how the Java virtual machine works with the computer's memory (RAM). The Java virtual machine is a whole computer model, so it naturally includes a memory model - AKA the Java memory model. It is vital to understand the Java memory model if you want to design correctly behaving concurrent programs. The Java memory model specifies how and when different threads can see values written to shared variables by other threads and how to synchronise access to shared variables when necessary. A memory model is best thought of as the architecture that determines how different parts of memory (i.e. stack and heap) interact. The Java Virtual Machine (JVM) divides the memory into two logical units: Thread stack Heap Thread stack The thread stack is the part of the memory that stores the data specific to a thread. Every thread has its own thread stack. The thread stack stores local variables (both primitive and reference variables) o...