0% found this document useful (0 votes)
12 views14 pages

Multithreading 2.3.1

The document provides an overview of Java threads, explaining their importance in concurrent programming and the basic thread lifecycle. It covers thread creation methods, thread priority, and synchronization to ensure data consistency. Additionally, it includes code examples demonstrating thread management and operations in Java.

Uploaded by

Jashika Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Multithreading 2.3.1

The document provides an overview of Java threads, explaining their importance in concurrent programming and the basic thread lifecycle. It covers thread creation methods, thread priority, and synchronization to ensure data consistency. Additionally, it includes code examples demonstrating thread management and operations in Java.

Uploaded by

Jashika Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIVERSITY INSTITUTE COMPUTING

BSc (CS)/ BCA


Subject Name: Java Programming
Code: 22SCT-351/22CAT-351

JAVA PROGRAMMING DISCOVER . LEARN . EMPOWER


1
Using finally Clause
• An Introduction to Java Threads
• Threads are a fundamental part of Java's concurrent programming
model. A thread is the smallest unit of execution within a program.
By using threads, you can perform multiple tasks concurrently,
making programs more efficient and responsive.
• What is a Thread?
• A thread is a lightweight process that runs within a larger process. It
has its own execution path but shares the process's resources such
as memory and files. Java provides built-in support for thread
creation and management, making it one of the most versatile
programming languages for concurrent applications.

2
• Benefits of Using Threads
• Concurrency: Allows multiple operations to run simultaneously.
• Resource Sharing: Threads within the same process can share resources,
reducing overhead.
• Responsive Applications: Enhances responsiveness, especially in GUI-based
applications.
• Parallel Processing: Makes use of multi-core processors to execute tasks in
parallel.

3
Basic Thread Lifecycle
• New: A thread object is created but not yet started.
• Runnable: The thread is ready to run and waiting for CPU time.
• Running: The thread is executing its task.
• Blocked/Waiting: The thread is paused and waiting for resources.
• Terminated: The thread has completed its execution.

4
Code Example
public class ThreadExample extends Thread {
public void run() {
System.out.println("Thread is running...");
}
public static void main(String[] args) {
ThreadExample thread = new ThreadExample();
thread.start(); // Start the thread
}
}

5
The Main Thread

In every Java program, the main thread is the first thread to execute. It is created automatically
when the program starts and serves as the entry point for all other threads.

Characteristics of the Main Thread


Default Thread: Automatically created by the JVM.
Primary Execution: Executes the main() method.
Parent Thread: Can create other threads.

6
Managing the Main Thread
Java provides methods to control the main thread using the Thread class. For example, you can change the thread's
name, priority, or state.

Code Example
public class MainThreadExample {
public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
System.out.println("Main thread name: " + mainThread.getName());

// Change the name of the main thread


mainThread.setName("PrimaryThread");
System.out.println("Renamed thread: " + mainThread.getName());

System.out.println("Main thread priority: " + mainThread.getPriority());


}
}
7
Java Thread Model
• Java Thread Model
• The Java Thread Model is based on the concept of multithreading, where a
program divides its tasks into multiple threads to execute them concurrently.
The model simplifies thread management by providing a platform-
independent way to create, control, and synchronize threads.
• Key Features of the Java Thread Model
• Preemptive Scheduling: Threads are scheduled based on their priority.
• Lightweight Threads: Java threads use fewer resources compared to
processes.
• Thread Synchronization: Ensures threads work harmoniously without data
inconsistencies.
• Platform Independence: The Java Virtual Machine (JVM) abstracts thread
management, making it consistent across operating systems.
8
Thread Creation
Threads in Java can be created in two primary ways:
Extending the Thread Class:

public class MyThread extends Thread {


public void run() {
System.out.println("Thread running by extending Thread class.");
}

public static void main(String[] args) {


MyThread thread = new MyThread();
thread.start();
}
}
9
Implementing the Runnable Interface:
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running by implementing Runnable interface.");
}

public static void main(String[] args) {


Thread thread = new Thread(new MyRunnable());
thread.start();
}
}

10
Thread Priority
Threads can have priorities ranging from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), with a default value of 5
(NORM_PRIORITY). Higher-priority threads are more likely to execute before lower-priority ones.

Code Example
public class ThreadPriorityExample {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> System.out.println("Thread 1 running..."));
Thread thread2 = new Thread(() -> System.out.println("Thread 2 running..."));
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MIN_PRIORITY);
thread1.start();
thread2.start();
}}

11
Thread Synchronization
Thread synchronization prevents multiple threads from accessing shared resources simultaneously, ensuring data
consistency.

Synchronized Method
class SharedResource {
synchronized void printNumbers() {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}}}}

12
public class SynchronizationExample {
public static void main(String[] args) {
SharedResource resource = new SharedResource();

Thread t1 = new Thread(() -> resource.printNumbers());


Thread t2 = new Thread(() -> resource.printNumbers());

t1.start();
t2.start();
}
}

13
THANK YOU

14

You might also like