Threading concept is very important in Java Programing language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime.
Threading in Java
- Overview of Thread
Threading concept is very important in Java Programing language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime.
- Life Cycle
of A Thread
Life Cycle of Thread contains different states - New state, Runnable, Running state, Dead state, Non-Runnable state.
- Thread Creation
Thread can be implementing by one of two ways - Extending the java.lang.Thread Class, Implementing the java.lang.Runnable Interface.
- Thread
Constructors
Several
constructors are available for creating new Thread instances like Thread() - Introduction
to Multithreading
Multithreading is a technique that allows a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single processor system.
- Creation
of Multiple Threads
Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing interface Runnable.
- Thread
Priorities and Scheduler
In Java, thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities.
- Deadlock
A situation where a thread is waiting for an object lock that holds by second thread, and this second thread is waiting for an object lock that holds by first thread, this situation is known as Deadlock.
- Lock and Synchronized Threads
Java uses monitor also known as ?semaphore? to prevent data from being corrupted by multiple threads by a keyword synchronized to synchronize them and intercommunicate to each other. Lock term refers to the access granted to a particular thread that can access the shared resources.
- Inter-Thread
Communication
A process where, a thread is paused while running in its critical region and another thread is allowed to enter (or lock) in the same critical section to be executed. This technique is known as Inter-Thread communication
- Daemon Thread
Daemon threads are service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing
- Thread Synchronization in Java
When two or more threads need shared resource, they need a proper mechanism to ensure that the resource will be used by only one thread at a time. The mechanism we use to achieve this is known as thread synchronization.