Oopjava Unit IV.pptx
Oopjava Unit IV.pptx
By
Dr. Srikanth Lakumarapu
Associate Professor
Dept. Of CSE
CVR COLLEE OF ENGINEERING
• What is Thread :
• A thread is a lightweight subprocess, the smallest unit of processing. It is a
separate path of execution.
Multiple Processes and Threads
Life cycle of Thread
• In Java, a thread always exists in any one of the following states.
• These states are:
1. New
2. Active
3. Blocked / Waiting
4. Timed Waiting
5. Terminated
Life cycle of Thread
Creation of a Thread
• There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Thread class
• Thread class is part of the java.lang package.
• It is used to create and manage threads in a program.
Key Constructors:
1. Thread(): Creates a new thread without specifying any target.
• When this thread's start() method is called, it executes the run() method of the
thread itself (i.e., the subclass's run() method if it's been overridden)
• Ex: Thread t = new Thread();
2. Thread(String name): Creates a new thread with a specified name, but
without any Runnable target.
• The thread will run its own run() method.
• Ex: Thread t = new Thread("MyThread");
Thread class Constructors
3. Thread(Runnable target): Creates a new thread that will run the run()
method of the given Runnable target object.
• Ex: Runnable r = new MyRunnable();
• Thread t = new Thread(r);
4. Thread(Runnable target, String name):Creates a new thread that will run
the run() method of the given Runnable target, and assigns a name to the thread.
• Ex: Thread t = new Thread(new MyRunnable(), "MyThread");
5. Thread(ThreadGroup group, String name): Creates a new thread within a
specified thread group, assigning the thread a name.
• Ex: ThreadGroup group = new ThreadGroup("MyGroup");
• Thread t = new Thread(group, "MyThread");
Thread class Constructors
6. Thread(ThreadGroup group, Runnable target): Creates a new thread
within a specified thread group. The thread will run the run() method of the given
Runnable target.
Ex: ThreadGroup group = new ThreadGroup("MyGroup");
Thread t = new Thread(group, new MyRunnable());
7. Thread(ThreadGroup group, Runnable target, String name): Creates a
new thread within a specified thread group, assigning the thread a name, and runs
the run() method of the provided Runnable target.
Ex: ThreadGroup group = new ThreadGroup("MyGroup");
Thread t = new Thread(group, new MyRunnable(), "MyThread");
Thread class methods
1. void start(): Starts the thread by making it runnable, which internally calls the
run() method.
2. void run(): Defines the task the thread will execute. You usually override this
method in a subclass or pass a Runnable to the Thread constructor.
3. static void sleep(long millis): Pauses the execution of the current thread for a
specified number of milliseconds.
4. void join(): Causes the current thread to wait until this thread finishes its execution.
5. void join(long millis): Causes the current thread to wait for this thread to finish or
for a specified number of milliseconds.
6. void interrupt(): Interrupts a thread that is currently sleeping, waiting, or otherwise
paused, causing it to throw an InterruptedException.
7. boolean isInterrupted(): Checks whether the thread has been interrupted, without
clearing the interrupt status.
Thread class methods
8. void setDaemon(boolean on): Marks a thread as a daemon thread, which
means it runs in the background and terminates when all user threads finish.
9. boolean isDaemon(): Checks if the thread is a daemon thread.
10. void setName(String name): Changes the name of the thread.
11. String getName(): Retrieves the name of the thread.
12. void yield(): Temporarily pauses the currently executing thread, giving other
threads a chance to execute.
13. void setPriority(int newPriority): Sets the priority of the thread (from 1
to 10, with higher values being more important).
14. int getPriority(): Returns the priority of the thread.
Thread class methods
15. Thread.State getState(): Retrieves the current state of the thread (e.g., NEW,
RUNNABLE, BLOCKED, WAITING, TERMINATED).
16. boolean isAlive(): Checks if the thread is still running (i.e., it has been started and
not yet terminated).
Thread Information and Utility Methods:
17. static Thread currentThread(): Returns a reference to the currently executing
thread.
19. static int activeCount(): Returns the number of active threads in the current
thread’s thread group.
Runnable interface
• The Runnable interface is a part of java.lang package.
• It is one of the key interfaces for implementing multithreading.
• When a class implements the Runnable interface, it provides a way to define a
task that can be run by a thread without extending the Thread class.
• This is beneficial because Java doesn’t support multiple inheritance, so by
implementing Runnable, a class can still extend another class.
• The Runnable interface is a functional interface with just one method:
public interface Runnable { public abstract void run(); }
Creating multiple Threads
• Multiple threads can be created by
• creating multiple threads of different thread classes
• Or
Annotations
Documentation Programming
Annotations Annotation