THREAD
* A thread is a lightweight sub-process or unit of execution within a program.
* Every Java program has at least one thread, the main thread, which is created by
the JVM when the program starts.
* Threads can be created explicitly to perform specific tasks concurrently.
Thread Lifecycle:
* New: When a thread object is created but not started.
* Runnable: After calling the start() method, the thread is ready to run but
waiting for CPU time.
* Running: The thread is executing its task.
* Blocked/Waiting: The thread is waiting for a resource or condition to be met.
* Terminated: The thread has finished its task or exited.
Java provides two primary ways to create threads:
* By extending the Thread class.
* By implementing the Runnable interface.
* Deamon thread:-The thread which are running at backend is called as Deamon
thread.
* MULTI THREADING:- Processing multiple threads simultaneously is called as multi -
threading.
* MULTI TASKING:- It is a Process of executing multiple task simultaneously is
called as multi-tasking.
* SYNCHRONIZED : Processing one by one thread is called as Synchronized .
* Non-SYNCHRONIZED :Processing multiple threads simultaneously is called as “non –
Synchronized“.
Method Description
___________________________________________________________________________________
______
start() Starts the thread and invokes the run() method.
run() Defines the code executed by the thread.
sleep(milliseconds) Pauses the thread for a specified duration.
getName() Returns the thread's name.
setName(name) Sets a name for the thread.
getId()
getPriority()
setPriority(int)
1. start()
* Starts the thread by creating a new execution path.
* Internally calls the run() method of the thread.
* You cannot call start() more than once on the same thread instance; doing so will
throw an IllegalThreadStateException.
2. run()
*Contains the code that the thread will execute.
*You can override this method in the Thread class or implement it in the Runnable
interface.
3. sleep(milliseconds)
* Pauses the current thread for a specified duration (in milliseconds).
* This is a static method of the Thread class.
* Throws InterruptedException if the thread is interrupted while sleeping.
4. getName()
* Retrieves the name of the thread.
* Every thread has a default name assigned by the JVM (e.g., Thread-0, Thread-1).
5. setName(name)
* Sets a custom name for the thread.
6. getId()
* Its ID will be generated by system
7. getPriority()
*the default priority will be five .
minimum is one and maximum is ten .
* Current thread is the static method of thread class which helps to return the
instance of currently executing thread
Wait
➢ It is non static method of object class.
➢ It is used only in the case of synchronize.
➢ It will wait still it receive notifications.
Sleep
➢ It is a static method thread class.
➢ It can be used in any code.
➢ It will pause its execution for few milliseconds.
Notify : It is the non - static method of object class which notifies the thread
which
is about access the resource.
Notify all : It is the non - static method of object class which will notify all
the
thread which is about access the resource